(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= |
+ | |author= |
|version=6.0 |
|version=6.0 |
||
|rating=4/6 |
|rating=4/6 |
||
Line 12: | Line 11: | ||
|category2= |
|category2= |
||
}} |
}} |
||
− | + | 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> |
||
⚫ | |||
− | " 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> |
||
⚫ | |||
⚫ | |||
− | 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(".")<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. |
||
⚫ | |||
− | ---- |
||
+ | {{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> |
||
⚫ | |||
− | </pre> |
||
− | |||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | ---- |
Revision as of 18:24, 11 April 2009
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?