Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add line numbers to range of lines in Vim?

How can I add line numbers to a range of lines in a file opened in Vim? Not as in :set nu—this just displays line numbers—but actually have them be prepended to each line in the file?

like image 217
dkretz Avatar asked Oct 31 '08 07:10

dkretz


People also ask

How do you set a range in vim?

One method is to use marks: Type ma in the first line, then type mb in the last line (to set marks a and b ). Then enter command :'a,'bs/old/new/g to substitute in lines from mark a to b , inclusive.

How do I create a numbered list in vim?

In Vim 8, the first number in a selection can be incremented by pressing Ctrl-A. If the selection covers several lines, the first number in the selection on each line is incremented. Alternatively, numbers in a selection covering several lines can be converted to a sequence by typing g Ctrl-A.


6 Answers

With

:%s/^/\=line('.')/

EDIT: to sum up the comments.

This command can be tweaked as much as you want.


Let's say you want to add numbers in front of lines from a visual selection (V + move), and you want the numbering to start at 42.

:'<,'>s/^/\=(line('.')-line("'<")+42)/

If you want to add a string between the number and the old text from the line, just concatenate (with . in VimL) it to the number-expression:

:'<,'>s/^/\=(line('.')-line("'<")+42).' --> '/

If you need this to sort as text, you may want to zero pad the results, which can be done using printf for 0001, 0002 ... instead of 1, 2... eg:

:%s/^/\=printf('%04d', line('.'))/

Anyway, if you want more information, just open vim help: :h :s and follow the links (|subreplace-special|, ..., |submatch()|)

like image 139
Luc Hermitte Avatar answered Oct 09 '22 04:10

Luc Hermitte


cat -n adds line numbers to its input. You can pipe the current file to cat -n and replace the current buffer with what it prints to stdout. Fortunately this convoluted solution is less than 10 characters in vim:

 :%!cat -n

Or, if you want just a subselection, visually select the area, and type this:

 :!cat -n

That will automatically put the visual selection markers in, and will look like this after you've typed it:

 :'<,'>!cat -n

In order to erase the line numbers, I recommend using control-v, which will allow you to visually select a rectangle, you can then delete that rectangle with x.

like image 23
Jerub Avatar answered Oct 09 '22 03:10

Jerub


On a GNU system: with the external nl binary:

:%!nl
like image 21
Martin v. Löwis Avatar answered Oct 09 '22 04:10

Martin v. Löwis


With Unix-like environment, you can use cat or awk to generate a line number easily, because vim has a friendly interface with shell, so everything work in vim as well as it does in shell.
From Vim Tip28:

:%!cat -n

or

:%!awk '{print NR,$0}'

But, if you use vim in MS-DOS, of win9x, win2000, you loss these toolkit. here is a very simple way to archive this only by vim:

fu! LineIt()
  exe ":s/^/".line(".")."/"
endf

Or, a sequence composed with alphabet is as easy as above:

exe "s/^/".nr2char(line("."))."/" 

You can also use a subst:

:g/^/exe ":s/^/".line(".")."^I/"

You can also only want to print the lines without adding them to the file:

"Sometimes it could be useful especially be editing large source files to print the line numbers out on paper.
To do so you can use the option :set printoptions=number:y to activate and :set printoptions=number:n to deactivate this feature.
If the line number should be printed always, place the line set printoptions=number:y in the vimrc."

like image 36
VonC Avatar answered Oct 09 '22 04:10

VonC


First, you can remove the existing line numbers if you need to:

:%s/^[0-9]*//

Then, you can add line numbers. NR refers to the current line number starting at one, so you can do some math on it to get the numbering you want. The following command gives you four digit line numbers:

:%!awk '{print 1000+NR*10,$0}'
like image 42
Lance Roberts Avatar answered Oct 09 '22 02:10

Lance Roberts


The "VisIncr" plugin is good for inserting columns of incrementing numbers in general (or letters, dates, roman numerals etc.). You can control the number format, padding, and so on. So insert a "1" in front of every line (via :s or :g or visual-block insert), highlight that column in visual-block mode, and run one of the commands from the plugin.

like image 45
Brian Carper Avatar answered Oct 09 '22 03:10

Brian Carper