Vim Tips Wiki
(Remove html character entities)
(clean up, using the best suggestion in the comments section)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=859
 
|id=859
Line 6: Line 5:
 
|created=January 21, 2005
 
|created=January 21, 2005
 
|complexity=basic
 
|complexity=basic
|author=shellreef
+
|author=
 
|version=6.0
 
|version=6.0
 
|rating=4/6
 
|rating=4/6
Line 12: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
Add this line to your vimrc to have Vim show how many lines you moved between when you hit the % key (which moves between matching pairs of characters, such as parenthesises and braces). The number of lines will be displayed at the bottom of the screen, positive for down, negative for up.
+
There are many situations where you may want to count the number of lines between matching pairs of characters, such as parenthesises and braces. This can be especially useful when programming Java, C++, Perl.
   
  +
Simply set the following:
Especially useful when programming Java, C++, Perl, as % easily lets you count how many lines are in a method, class, or clause. Note that ^M needs to be typed as Ctrl+V, Ctrl+M.
 
   
 
<pre>
 
<pre>
 
:set showcmd
" Count number of lines within this matching pair, print them
 
" when jumping between braces with %. Very useful. By shellreef.
 
nnoremap % :let line=line(".")^M%:echo line(".") - line^M
 
 
</pre>
 
</pre>
   
 
After this, in bottomright corner of vim window you will see count of lines currently selected in visual mode (if there are any), among many other useful things.
==Comments==
 
Good tip but the mapping woun't work. The problem is you are setting line and retrieving the actual line without moving the cursor. This would work better:
 
   
 
Now, to see how many lines are between pairs, you set cursor on first pair, enter visual mode and press %. Now in bottom right corner you see desired number.
<pre>
 
let showline=0
 
nnoremap ! :let newline=line(".")<CR>:let numlines=newline-showline<CR>:let showline=newline<CR>:echo numlines<CR>
 
</pre>
 
   
  +
Or, for certain matched pairs like {...} groups in a C program, you can use a text object. For example, in C code, anywhere within matched braces, enter visual mode and press "iB" to select everything between the braces. The bottom right corner contains the desired count.
----
 
The % moves the cursor; the mapping works for me. I could see how your mapping would be useful to mark a line, move to another line, and find out how many lines you moved--a more general case than mine.
 
   
 
==Comments==
----
 
  +
{{todo}}
Another way to do the same is folowing:
 
  +
*Rename tip to make it a more generic tip on the <tt>'showcmd'</tt> option?
 
<pre>
 
:set showcmd
 
</pre>
 
 
After this, in bottomright corner of vim window you will see count of lines currently selected (if there are any). And many other useful things too. :)
 
 
Now, to see how many lines is between pairs, you set cursor on first pair, enter visual mode and press %. Now in bottomright corner you see desired number.
 
 
----
 

Revision as of 18:24, 11 April 2009

Tip 859 Printable Monobook Previous Next

created January 21, 2005 · complexity basic · version 6.0


There are many situations where you may want to count the number of lines between matching pairs of characters, such as parenthesises and braces. This can be especially useful when programming Java, C++, Perl.

Simply set the following:

:set showcmd

After this, in bottomright corner of vim window you will see count of lines currently selected in visual mode (if there are any), among many other useful things.

Now, to see how many lines are between pairs, you set cursor on first pair, enter visual mode and press %. Now in bottom right corner you see desired number.

Or, for certain matched pairs like {...} groups in a C program, you can use a text object. For example, in C code, anywhere within matched braces, enter visual mode and press "iB" to select everything between the braces. The bottom right corner contains the desired count.

Comments

 TO DO 

  • Rename tip to make it a more generic tip on the 'showcmd' option?