add line number in a file using vim

I just needed to insert line numbers in a file in vim. Thanks to 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 comment will only be visible after moderation. Your email address will not be published.