开发者

Simple Vim Programming (vimrc file)

I'm trying to learn how to configure my .vimrc file with my own functions.

I'd like to write a function that traverses every line in a file and counts the total number of characters, but ignores all whitespace. This is for a programming exercise and as a stepping stone to more complex programs (I know there are other ways to get this example value using Vim or external programs).

Here's what I have so far:

function countchars()
    let line = 0
    le开发者_如何学Pythont count = 0
    while line < line("$")
        " update count here, don't count whitespace
        let line = getline(".")
    return count
endfun

What functional code could I replace that commented line with?


If I understand the question correctly, you're looking to count the number of non-whitespace characters in a line. A fairly simple way to do this is to remove the whitespace and look at the length of the resulting line. Therefore, something like this:

function! Countchars()
    let l = 1
    let char_count = 0
    while l <= line("$")
        let char_count += len(substitute(getline(l), '\s', '', 'g'))
        let l += 1
    endwhile
    return char_count
endfunction

The key part of the answer to the question is the use of substitute. The command is:

substitute(expr,pattern,repl,flags)

expr in this case is getline(l) where l is the number of the line being iterated over. getline() returns the content of the line, so this is what is being parsed. The pattern is the regular expression \s which matches any single whitespace character. It is replaced with '', i.e. an empty string. The flag g makes it repeat the substitute as many times as whitespace is found on the line.

Once the substitution is complete, len() gives the number of non-whitespace characters and this is added to the current value of char_count with +=.


A few things that I've changed from your sample:

  • The function name starts with a capital letter (this is a requirement for user defined functions: see :help user-functions)
  • I've renamed count to char_count as you can't have a variable with the same name as a function and count() is a built-in function
  • Likewise for line: I renamed this to l
  • The first line in a file is line 1, not line 0, so I initialised l to 1
  • The while loop counted up to but not including the last line, I assume you wanted all the lines in the file (this is probably related to the line numbering starting at 1): I changed your code to use <= instead of <
  • Blocks aren't based on indentation in vim, so the while needs an endwhile
  • In your function, you have let line = getline('.')
  • I added a ! on the function definition as it makes incremental development much easier (everytime you re-source the file, it will override the function with the new version rather than spitting out an error message about it already existing).
  • Incrementing through the file works slightly differently...

In your function, you had let line = getline('.'). Ignoring the variable name, there are still some problems with this implementation. I think what you meant was let l = line('.'), which gives the line number of the current line. getline('.') gives the contents of the current line, so the comparison on the while line would be comparing the content of the current line with the number of the last line and this would fail. The other problem is that you're not actually moving through the file, so the current line would be whichever line you were on when you called the function and would never change, resulting in an infinite loop. I've replaced this with a simple += 1 to step through the file.

There are ways in which the current line would be a useful way to do this, for example if you were writing a function with that took a range of lines, but I think I've written enough for now and the above will hopefully get you going for now. There are plenty of people on stackoverflow to help with any issues anyway!

Have a look at:

:help usr_41.txt
:help function-list
:help user-functions
:help substitute()

along with the :help followed by the various things I used in the function (getline(), line(), let+= etc).

Hope that was helpful.


This approach uses lists:

function! Countchars()
  let n = 0
  for line in getline(1,line('$'))
    let n += len(split(line,'\zs\s*'))
  endfor
  return n
endfunction


I suppose you have already found the solution.

Just for info: I use this to count characters without spaces in Vim: %s/\S/&/gn

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜