Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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:</p> <pre><code>function! Countchars() let l = 1 let char_count = 0 while l &lt;= line("$") let char_count += len(substitute(getline(l), '\s', '', 'g')) let l += 1 endwhile return char_count endfunction </code></pre> <p>The key part of the answer to the question is the use of substitute. The command is:</p> <pre><code>substitute(expr,pattern,repl,flags) </code></pre> <p><code>expr</code> in this case is <code>getline(l)</code> where <code>l</code> is the number of the line being iterated over. <code>getline()</code> returns the content of the line, so this is what is being parsed. The pattern is the regular expression <code>\s</code> which matches any single whitespace character. It is replaced with <code>''</code>, i.e. an empty string. The flag <code>g</code> makes it repeat the substitute as many times as whitespace is found on the line.</p> <p>Once the substitution is complete, <code>len()</code> gives the number of non-whitespace characters and this is added to the current value of <code>char_count</code> with <code>+=</code>.</p> <hr> <p>A few things that I've changed from your sample:</p> <ul> <li>The function name starts with a capital letter (this is a requirement for user defined functions: see <code>:help user-functions</code>)</li> <li>I've renamed <code>count</code> to <code>char_count</code> as you can't have a variable with the same name as a function and <code>count()</code> is a built-in function</li> <li>Likewise for <code>line</code>: I renamed this to <code>l</code></li> <li>The first line in a file is line 1, not line 0, so I initialised <code>l</code> to 1</li> <li>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 <code>&lt;=</code> instead of <code>&lt;</code></li> <li>Blocks aren't based on indentation in vim, so the <code>while</code> needs an <code>endwhile</code></li> <li>In your function, you have <code>let line = getline('.')</code></li> <li>I added a <code>!</code> on the <code>function</code> 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).</li> <li>Incrementing through the file works slightly differently...</li> </ul> <p>In your function, you had <code>let line = getline('.')</code>. Ignoring the variable name, there are still some problems with this implementation. I think what you <em>meant</em> was <code>let l = line('.')</code>, which gives the line number of the current line. <code>getline('.')</code> 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 <code>+= 1</code> to step through the file.</p> <p>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!</p> <p>Have a look at:</p> <pre><code>:help usr_41.txt :help function-list :help user-functions :help substitute() </code></pre> <p>along with the <code>:help</code> followed by the various things I used in the function (<code>getline()</code>, <code>line()</code>, <code>let+=</code> etc).</p> <p>Hope that was helpful.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload