Tip #28: add a line-number to every line without cat or awk alike utilities.
tip karma |
Rating 176/67, Viewed by 10985
|
Read and edit this tip on the
Vim tip wiki.
The wiki may have a more recent version of this tip.
created: |
|
March 7, 2001 5:08 |
|
complexity: |
|
intermediate |
author: |
|
[email protected] |
|
as of Vim: |
|
5.7 |
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.
:%!call -n
or
:%!awk '{print NR,$0}'
But, if you use vim in MS-DOS, of win9x, win2000, you loss these tookit.
here is a very simple way to archive this only by vim:
fu! LineIt()
exe ":s/^/".line(".")."/"
endf
Well, a sequence composed with alphabet is as easy as above:
exe "s/^/".nr2char(line("."))."/"
<< Convert hex to dec |
reverse all the line with only 7 keystroke in vim >>
Additional Notes
darren chamberlain <[email protected]>,
July 2, 2001 4:50
|
The original awk line gave me entries like:
8 my $foo = Foo->new();
9 my $bar = Bar->new();
10 my $baz = Baz->new();
Turning awk's print to a printf and adding a size the line number format gave me better results:
8 my $foo = Foo->new();
9 my $bar = Bar->new();
10 my $baz = Baz->new();
:%!awk '{printf "\%3d \%s\n", NR, $0}'
|
[email protected],
October 12, 2001 16:52
|
When I used this, the syntax highlighting for some terms went OFF,
(for example, #include, #define....those that are meant to be in col.1)
anyways to fix that ?
tia,
krishna.
|
[email protected],
October 18, 2002 7:51
|
:set number
|
[email protected],
July 8, 2003 2:49
|
One could also use unix's `nl` command to add numbers to all lines.
Example usage: :%nl -ba
|
[email protected],
November 28, 2003 16:59
|
:se nu
|
[email protected],
October 31, 2004 21:03
|
:%! nl -ba
or
:%! cat -n
|
Anonymous,
March 18, 2005 14:43
|
the issue is whether or not the hardcopy also contains the line numbers.
|
Anonymous,
March 18, 2005 15:17
|
exe ":s/^/".line(".")."/" only appears to work on the very line i'm on, not all of the lines.
exe ":%s/^/".line(".")."/" just puts the line number on all the lines.
is there anyway to execute the command on all the lines?
exe ":s/^/".line(".")."/g" does not work either!
|
Anonymous,
March 18, 2005 15:38
|
sadly, i did not find the way to "execute" multiple times; however if you put it in a function as he did above, you can call functions multple times. an independent one liner would be nice..
:%call LineIt()
|
Anonymous,
April 28, 2005 9:05
|
thanks...
I found a way to add line number to entire file
:g/^/exe ":s/^/".line(".")."^I/"
|
[email protected],
June 28, 2005 21:22
|
Printing with line numbers:
If you're trying to print a file with line numbers, add/modify/alter such that the option "number:y" is in your 'printoptions' string.
|
Anonymous,
June 5, 2006 7:33
|
just make it clear that how to print line number in vim. The following is from online book "VIM Best Practices":
"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."
|
Anonymous,
June 26, 2006 12:52
|
the wicked anonymous genius who came up with the option to add line numbers - thanks a lot.
I also found a way to do it at the end of line. here is my snippet.
:g/$/exe ":s/$/".line(".").";/"
|
|