Vim Tips Wiki
m (typo)
(Uploaded by JohnBot from a locally edited file)
Line 1: Line 1:
  +
{{TipImported
{{Tip
 
 
|id=124
 
|id=124
  +
|previous=123
|title=Number a group of lines
 
  +
|next=125
|created=October 1, 2001 16:37
+
|created=October 1, 2001
 
|complexity=basic
 
|complexity=basic
 
|author=Brian Medley
 
|author=Brian Medley
 
|version=5.7
 
|version=5.7
 
|rating=6/3
 
|rating=6/3
 
}}
|text=
 
Below is a way to number a set of lines. Here is an example before and after snapshot:
+
Below is a way to number a set of lines. Here is an example before and after snapshot:
   
  +
<pre>
apple
+
apple
bob
+
bob
pear
+
pear
tree
+
tree
   
1 apple
+
1 apple
2 bob
+
2 bob
3 pear
+
3 pear
4 tree
+
4 tree
  +
</pre>
   
===Description: ===
+
===Description===
This provides a command and a function. They both can be called with or without a range. In addition, they can be called with or without arguments. Without a range they operate on the current line.
+
This provides a command and a function. They both can be called with or without a range. In addition, they can be called with or without arguments. Without a range they operate on the current line.
   
 
There are two supported arguments. They are described below:
   
 
* '''arg1''' the number to start at. The default is one. This will
There are two supported arguments. They are described below:
 
 
number your selected lines sequentially. The start can be a
 
number, ., $, or, 'x (like getline).
   
* '''arg1''' the number to start at. The default is one. This will
+
* '''arg2''' Text to append after numbers. The default is a space.
number your selected lines sequentially. The start can be a
 
number, ., $, or, 'x (like getline).
 
   
 
===Examples===
* '''arg2''' Text to append after numbers. The default is a space.
 
 
To provide your functionality:
   
  +
<pre>
===Examples: ===
 
 
:%Nlist 20
 
:%call Nlist(20)
  +
</pre>
   
 
To make a list start at 1:
To provide your functionality:
 
   
  +
<pre>
:%Nlist 20
 
:%call Nlist(20)
+
:'&lt;,'&gt;Nlist
 
:'&lt;,'&gt;call Nlist()
  +
</pre>
   
  +
To number the whole buffer (with it's actual line number):
To make a list start at 1:
 
   
  +
<pre>
:'&lt;,'&gt;Nlist
 
:'&lt;,'&gt;call Nlist()
+
:%Nlist
  +
:%call Nlist()
  +
</pre>
   
To number the whole buffer (with it's actual line number):
+
To number a subset of lines with their line number (and put a '] ' in
 
front of every number):
   
  +
<pre>
:%Nlist
 
:%call Nlist()
+
:'&lt;,'&gt;Nlist . ]\
 
:'&lt;,'&gt;call Nlist(".", "] ")
   
 
command! -nargs=* -range Nlist &lt;line1&gt;,&lt;line2&gt;call Nlist(&lt;f-args&gt;)
To number a subset of lines with their line number (and put a '] ' in
 
  +
</pre>
front of every number):
 
 
:'&lt;,'&gt;Nlist . ]\
 
:'&lt;,'&gt;call Nlist(".", "] ")
 
 
command! -nargs=* -range Nlist &lt;line1&gt;,&lt;line2&gt;call Nlist(&lt;f-args&gt;)
 
   
 
<pre>
 
<pre>
function! Nlist(...) range
+
function! Nlist(...) range
  +
if 2 == a:0
 
if 2 == a:0
+
let start = a:1
let start = a:1
+
let append = a:2
let append = a:2
+
elseif 1 == a:0
elseif 1 == a:0
+
let start = a:1
let start = a:1
+
let append = " "
  +
else
let append = " "
 
 
let start = 1
else
 
 
let append = " "
 
  +
endif
let start = 1
 
 
" try to work like getline (i.e. allow the user to pass in . $ or 'x)
let append = " "
 
 
if 0 == (start + 0)
endif
 
 
let start = line(start)
 
  +
endif
 
 
exe a:firstline . "," . a:lastline . 's/^/\=line(".")-a:firstline+start.append/'
" try to work like getline (i.e. allow the user to pass in . $ or 'x)
 
 
endfunction
 
if 0 == (start + 0)
 
let start = line(start)
 
endif
 
 
exe a:firstline . "," . a:lastline . 's/^/\=line(".")-a:firstline+start.append/'
 
 
endfunction
 
 
</pre>
 
</pre>
   
 
==Comments==
}}
 
 
There is now a plugin {{script|id=101|text=nlist.vim}} that does this. The plugin also handles justifying the numbers. I probably should have waited and not made this tip.
   
== Comments ==
 
There is now a plugin # {{script|id=101|text=nlist.vim}} in the "scripts" section that does this. The plugin also handles justifying the numbers. I probably should have waited and not made this tip...
 
 
Brian Medley, October 1, 2001 22:25
 
 
----
 
----
 
 
[[Category:Scripting]]
 
[[Category:Scripting]]

Revision as of 10:41, 30 October 2007

Tip 124 Printable Monobook Previous Next

created October 1, 2001 · complexity basic · author Brian Medley · version 5.7


Below is a way to number a set of lines. Here is an example before and after snapshot:

apple
bob
pear
tree

1 apple
2 bob
3 pear
4 tree

Description

This provides a command and a function. They both can be called with or without a range. In addition, they can be called with or without arguments. Without a range they operate on the current line.

There are two supported arguments. They are described below:

  • arg1 the number to start at. The default is one. This will

number your selected lines sequentially. The start can be a number, ., $, or, 'x (like getline).

  • arg2 Text to append after numbers. The default is a space.

Examples

To provide your functionality:

:%Nlist 20
:%call Nlist(20)

To make a list start at 1:

:'<,'>Nlist
:'<,'>call Nlist()

To number the whole buffer (with it's actual line number):

:%Nlist
:%call Nlist()

To number a subset of lines with their line number (and put a '] ' in front of every number):

:'<,'>Nlist . ]\
:'<,'>call Nlist(".", "] ")

command! -nargs=* -range Nlist <line1>,<line2>call Nlist(<f-args>)
function! Nlist(...) range
  if 2 == a:0
    let start = a:1
    let append = a:2
  elseif 1 == a:0
    let start = a:1
    let append = " "
  else
    let start = 1
    let append = " "
  endif
  " try to work like getline (i.e. allow the user to pass in . $ or 'x)
  if 0 == (start + 0)
    let start = line(start)
  endif
  exe a:firstline . "," . a:lastline . 's/^/\=line(".")-a:firstline+start.append/'
endfunction

Comments

There is now a plugin nlist.vim that does this. The plugin also handles justifying the numbers. I probably should have waited and not made this tip.