add line number in a file using vim

I just needed to insert line numbers in a file in vim. Thanks to https://vi.stackexchange.com/questions/8179/replace-a-pattern-with-current-line-number I now know how to do this by replacing the start of every line using the following command:

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

which adds the current line number formatted to 4 characters with leading spaces.

Leave a Reply

Your email address will not be published.