Vim: insert line numbers

Posted by: Brad Waite

Tagged in: vim , search and replace

To insert line numbers at the beginning of every line in Vim, do this:


This is a global search and replace (%s/[search]/[replace]), matching the beginning of each line (^) with an expression (\=) that returns the current line number (line(".")).

To add a comma (or any other character) after the line number, do it like this:

:%s/^/\=line(".") . ","

Any valid Vim expression is valid after the "\=", so you could start line number with 100 like this:


Pretty simple and straightforward, when you think about it.

Comments (0)Add Comment

Write comment
You must be logged in to post a comment. Please register if you do not have an account yet.