Vim Tips Wiki
(Duplicate and slight clean)
(Change <tt> to <code>, perhaps also minor tweak.)
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Duplicate|65}}
 
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=28
 
|id=28
 
|previous=27
 
|previous=27
 
|next=29
 
|next=29
|created=March 7, 2001
+
|created=2001
 
|complexity=intermediate
 
|complexity=intermediate
|author=slimzhao
+
|author=
|version=5.7
+
|version=7.0
 
|rating=159/57
 
|rating=159/57
  +
|category1=
  +
|category2=
 
}}
 
}}
  +
The <code>:set number</code> command displays line numbers. This tip, however, shows how to insert line numbers into a file, or into just a section. Also, the option to print with line numbers is given (Vim can print lines with numbers, and does not need the numbers in the file).
Under a Unix-like environment, you can use cat or awk to easily insert line numbers into a file.
 
   
  +
==Using Vim==
  +
The [[Search and replace|:s]] command can be used to insert line numbers before each line:
 
<pre>
 
<pre>
  +
:%s/^/\=printf('%-4d', line('.'))
:%!cat -n
 
or
 
:%!awk '{print NR,$0}'
 
 
</pre>
 
</pre>
   
  +
The pattern <code>^</code> matches the start of every line, and the replacement <code>\=</code> is the result of evaluating the following expression. That expression uses <code>printf()</code> to format the number of the current line: <code>%-4d</code> is a left-aligned decimal number, padded if necessary by adding spaces to a 4-column width (<code>%4d</code> is right-aligned, and <code>%04d</code> inserts leading zeroes).
Under Windows, you can use the following Vim function to insert line numbers.
 
   
  +
To number a section, specify which lines should be numbered with a [[Ranges|range]]. For example, press <code>V</code> to select the first line, then press <code>j</code> to extend the selection down until all required lines are selected. Then type the following command (do not type <code>'<,'></code> as that is inserted by Vim):
 
<pre>
 
<pre>
  +
:'<,'>s/^/\=printf("%d.\t", line(".") - line("'<") + 1)
fu! LineIt()
 
exe ":s/^/".line(".")."/"
 
endf
 
 
</pre>
 
</pre>
   
  +
The above example has a format string of <code>"%d.\t"</code> which inserts a left-aligned number, followed by a period and a tab character. Each line in the selected range is numbered. Use the following if you want to only number non-blank lines (it finds the start of a line followed by a character that is not whitespace):
A sequence composed with the alphabet is as easy as above:
 
  +
<pre>
  +
:'<,'>s/^\S/\=printf("%d.\t", line(".") - line("'<") + 1)
  +
</pre>
   
  +
In the above, blank lines are counted, but do not have a number inserted (the inserted numbers would be 1, 2, 3, 5, 6 if line 4 was blank).
exe "s/^/".nr2char(line("."))."/"
 
   
  +
The following alternative does not number blank lines and does not skip line numbers:
==Comments==
 
  +
<pre>
The original awk line gave me entries like:
 
  +
:'<,'>g/^\S/s/^/\=printf("%d.\t", Inc())
  +
</pre>
   
  +
The above requires the <code>Inc()</code> function from [[Making a list of numbers|here]]. In addition, you need to set the value for the first line number before selecting any lines. To do that enter:
 
<pre>
 
<pre>
  +
:let i = 1
8 my $foo = Foo-&gt;new();
 
9 my $bar = Bar-&gt;new();
 
10 my $baz = Baz-&gt;new();
 
 
</pre>
 
</pre>
   
  +
==Using <code>nl</code>==
Using printf gave me better results:
 
  +
On Unix based systems, the ''number lines'' utility can be used to insert numbers by filtering the whole buffer:
 
 
<pre>
 
<pre>
 
:%!nl -ba
8 my $foo = Foo-&gt;new();
 
9 my $bar = Bar-&gt;new();
 
10 my $baz = Baz-&gt;new();
 
 
</pre>
 
</pre>
   
  +
The <code>nl</code> utility has many options to control which lines are numbered and how the numbers are formatted.
:%!awk '{printf "\%3d \%s\n", NR, $0}'
 
   
  +
To number a section, specify which lines should be numbered with a [[Ranges|range]]. For example, press <code>V</code> to select the first line, then press <code>j</code> to extend the selection down until all required lines are selected. Then type <code>:!nl -ba</code>. You will see (the <code>'<,'></code> is inserted by Vim):
----
 
  +
<pre>
When I used this, the syntax highlighting for some terms went OFF,
 
 
:'<,'>!nl -ba
(for example, #include, #define....those that are meant to be in col.1)
 
  +
</pre>
   
 
==Printing with line numbers==
----
 
  +
There is no need to insert line numbers if all that is wanted is to have lines numbered in a printout. Instead, use the following command to tell Vim to insert line numbers on printing (change <code>y</code> to <code>n</code> to turn this off):
One could also use unix's `nl` command to add numbers to all lines.
 
  +
<pre>
Example usage: :%nl -ba
 
  +
:set printoptions=number:y
  +
</pre>
   
 
==Comments==
----
 
  +
[[VimTip569|Insert line numbers with a Perl filter]] is another method.
:%! nl -ba
 
or
 
:%! cat -n
 
 
----
 
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!
 
 
----
 
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()
 
 
----
 
I found a way to add line number to entire file
 
 
:g/^/exe ":s/^/".line(".")."^I/"
 
 
----
 
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.
 
 
----
 
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."
 
 
----
 
I also found a way to do it at the end of line. here is my snippet.
 
 
:g/$/exe ":s/$/".line(".").";/"
 
 
----
 

Revision as of 12:28, 15 July 2012

Tip 28 Printable Monobook Previous Next

created 2001 · complexity intermediate · version 7.0


The :set number command displays line numbers. This tip, however, shows how to insert line numbers into a file, or into just a section. Also, the option to print with line numbers is given (Vim can print lines with numbers, and does not need the numbers in the file).

Using Vim

The :s command can be used to insert line numbers before each line:

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

The pattern ^ matches the start of every line, and the replacement \= is the result of evaluating the following expression. That expression uses printf() to format the number of the current line: %-4d is a left-aligned decimal number, padded if necessary by adding spaces to a 4-column width (%4d is right-aligned, and %04d inserts leading zeroes).

To number a section, specify which lines should be numbered with a range. For example, press V to select the first line, then press j to extend the selection down until all required lines are selected. Then type the following command (do not type '<,'> as that is inserted by Vim):

:'<,'>s/^/\=printf("%d.\t", line(".") - line("'<") + 1)

The above example has a format string of "%d.\t" which inserts a left-aligned number, followed by a period and a tab character. Each line in the selected range is numbered. Use the following if you want to only number non-blank lines (it finds the start of a line followed by a character that is not whitespace):

:'<,'>s/^\S/\=printf("%d.\t", line(".") - line("'<") + 1)

In the above, blank lines are counted, but do not have a number inserted (the inserted numbers would be 1, 2, 3, 5, 6 if line 4 was blank).

The following alternative does not number blank lines and does not skip line numbers:

:'<,'>g/^\S/s/^/\=printf("%d.\t", Inc())

The above requires the Inc() function from here. In addition, you need to set the value for the first line number before selecting any lines. To do that enter:

:let i = 1

Using nl

On Unix based systems, the number lines utility can be used to insert numbers by filtering the whole buffer:

:%!nl -ba

The nl utility has many options to control which lines are numbered and how the numbers are formatted.

To number a section, specify which lines should be numbered with a range. For example, press V to select the first line, then press j to extend the selection down until all required lines are selected. Then type :!nl -ba. You will see (the '<,'> is inserted by Vim):

:'<,'>!nl -ba

Printing with line numbers

There is no need to insert line numbers if all that is wanted is to have lines numbered in a printout. Instead, use the following command to tell Vim to insert line numbers on printing (change y to n to turn this off):

:set printoptions=number:y

Comments

Insert line numbers with a Perl filter is another method.