Avatar
Linux, plaintext & minimalism
August 27, 2018

Adding line numbers to a list in Vim with awk

I have finally found a good way of adding line numbers to a list in Vim. Most tips I’ve found so far has been overly complicated and with various success, this option uses awk and it’s in return a bit simpler.

:'<,'>%!awk '{print NR".",$0}'

It works by you selecting the lines with Ctrl+v and then execute the command, it will then transform your list from this:

One
Two
Three

To this

1. One
2. Two
3. Three

And because I don’t want to manually type it every time, I decided to map it to \n:

map \n :'<,'>%!awk '{print NR".",$0}'<CR>

Source: Stackoverflow

Tags: Vim

Feedback

I don't have a commenting system simply because I haven't found a solution that I like. If you want to leave any feedback and/or have any questions, feel free to contact me either via e-mail, fediverse, XMPP or IRC (I'm Hund at freenode.net).