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
let 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:
:help user-functions
)count
to char_count
as you can't have a variable with the same name as a function and count()
is a built-in functionline
: I renamed this to l
l
to 1<=
instead of <
while
needs an endwhile
let line = getline('.')
!
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).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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With