Simple Vim Programming (vimrc file)

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

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

Here is what I still have:

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 can I replace this commented line with?

+4
source share
3 answers

If I understand the question correctly, you expect to count the number of characters without spaces in the string. A fairly simple way to do this is to remove the spaces and see the length of the resulting string. So 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 

A key part of answering the question is the use of substitution. Command:

 substitute(expr,pattern,repl,flags) 

expr in this case getline(l) , where l is the number of the line renamed. getline() returns the contents of the string, so this is what is parsed. A pattern is a \s regular expression that matches any single space character. It is replaced by '' , i.e. An empty string. The g flag allows you to repeat the replacement as many times as there are spaces found in the line.

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


A few things I changed from your example:

  • The name of the function begins with a capital letter (this is a requirement for user-defined functions: see :help user-functions )
  • I renamed count to char_count , since you cannot have a variable with the same name as the function, and count() is a built-in function
  • Similarly for line : I renamed it to l
  • The first line in the file is line 1, not line 0, so I initialized l to 1
  • The while loop counts to the last line, but does not include the last line. I assume that you need all the lines in the file (this is probably due to line numbering starting from 1): I changed the code to use <= instead of of <
  • Blocks are not indented in vim, so while requires endwhile
  • In your function, you have let line = getline('.')
  • I have added ! in the definition of function , since it makes incremental development much easier (every time you re-upload the file, it will override the function with the new version, rather than spit out an existing error message).
  • The increment through the file works a little differently ...

In your function, you have let line = getline('.') . Ignoring the variable name, there are still some problems with this implementation. I think you meant let l = line('.') , Which gives the line number of the current line. getline('.') gives the contents of the current line, so comparing the while line will compare the contents of the current line with the number of the last line, and this will not work. Another problem is that you are not actually navigating the file, so the current line will depend on which line you were when you called the function and never changed, which will lead to an infinite loop. I replaced this with a simple += 1 to execute the file.

There are ways in which the current line would be a useful way to do this, for example, if you wrote some function that occupied a number of lines, but I think I wrote enough at the moment, and the above will hope you go now . There are many people at stackoverflow to help with any problems anyway!

Take a look:

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

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

Hope this was helpful.

+8
source

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 
+3
source

I suppose you have already found a solution.

For information only: I use this to count characters without spaces in Vim: %s/\S/&/gn

+3
source

Source: https://habr.com/ru/post/1343550/


All Articles