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:
The key part of the answer to the question is the use of substitute. The command is:
exprin this case isgetline(l)wherelis 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\swhich matches any single whitespace character. It is replaced with'', i.e. an empty string. The flaggmakes 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 ofchar_countwith+=.A few things that I’ve changed from your sample:
:help user-functions)counttochar_countas you can’t have a variable with the same name as a function andcount()is a built-in functionline: I renamed this tollto 1<=instead of<whileneeds anendwhilelet line = getline('.')!on thefunctiondefinition 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 waslet 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+= 1to 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:
along with the
:helpfollowed by the various things I used in the function (getline(),line(),let+=etc).Hope that was helpful.