Vim Tips Wiki
m (Remap % to show how many lines in between pairs moved to Show how many lines between matching pairs: Page moved by JohnBot to improve title)
(Change to TipImported template + severe manual clean)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=859
 
|id=859
  +
|previous=858
|title=Remap % to show how many lines in between pairs
 
  +
|next=860
|created=January 21, 2005 19:48
+
|created=January 21, 2005
 
|complexity=basic
 
|complexity=basic
 
|author=shellreef
 
|author=shellreef
 
|version=6.0
 
|version=6.0
 
|rating=4/6
 
|rating=4/6
 
}}
|text=
 
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.
+
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.
   
 
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>
 
" 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>
   
 
==Comments==
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.
 
 
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:
   
  +
<pre>
 
let showline=0
 
nnoremap ! :let newline=line(".")&lt;enter&gt;:let numlines=newline-showline&lt;enter&gt;:let showline=newline&lt;enter&gt;:echo numlines&lt;enter&gt;
  +
</pre>
   
 
----
 
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.
   
 
----
" Count number of lines within this matching pair, print them
 
 
Another way to do the same is folowing:
   
  +
<pre>
" when jumping between braces with %. Very useful. By shellreef.
 
 
:set showcmd
  +
</pre>
   
nnoremap % :let line=line(".")^M%:echo line(".") - line^M
 
 
 
 
 
}}
 
 
== Comments ==
 
Good tip but the mapping woun't work. The problem is you are setting line and retreaving the actual line withoup moving the cursor. This would work better:
 
----------------------------
 
let showline=0
 
nnoremap ! :let newline=line(".")&lt;enter&gt;:let numlines=newline-showline&lt;enter&gt;:let showline=newline&lt;enter&gt;:echo numlines&lt;enter&gt;
 
----------------------------
 
 
'''Anonymous'''
 
, January 24, 2005 4:27
 
----
 
Another way to do the same is folowing:
 
:set showcmd
 
 
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. :)
 
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. :)
   
t7ko--AT--mail.ru
 
, January 24, 2005 5:15
 
----
 
Ops... forgot to append this:
 
 
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.
 
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.
   
t7ko--AT--mail.ru
 
, January 24, 2005 5:17
 
----
 
Anonymous: 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. Thanks for sharing it, I'd add to my vimrc but I use ! a lot...
 
 
t7ko--cool, never knew how useful visual mode could be. Thanks.
 
 
'''Anonymous'''
 
, February 19, 2005 23:38
 
 
----
 
----
<!-- parsed by vimtips.py in 0.460222 seconds-->
 

Revision as of 10:03, 1 December 2007

Tip 859 Printable Monobook Previous Next

created January 21, 2005 · complexity basic · author shellreef · version 6.0


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.

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.

" 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

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:

let showline=0
nnoremap ! :let newline=line(".")<enter>:let numlines=newline-showline<enter>:let showline=newline<enter>:echo numlines<enter>

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.


Another way to do the same is folowing:

:set showcmd

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.