27 February 2018

How to insert line numbers to a Vim file

How to insert line numbers to a Vim file?

Insert line numbers

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).

No comments:

Post a Comment