Lately I posted on how to show line numbers in Vi editor, but have you ever wishes that with a single key you are able to do much more in Vi editor ? For instance, toggle show/hide line numbers in Vi editor.

Yes, it is possible with the .vimrc file to map keys so that they perform certain pre-defined action when pressed. Let’s see how is it done.

Open terminal window and type the following command.

vi ~/.vimrc

This will open the .vimrc file which is kept in your home directory. If you or your administrator had made some changes, then make sure you backup those. We will set key “F2” to show line numbers, and key “F3” to hide them.

map #2 :set number

map #3 :set nonumber

Alternatively, as short code you can also write the above lines as :

map #2 :set nu

map #3 :set nonu

When you have these two lines in place, press ESC key and then “wq” to write and exit the file.

That should do the trick, Now when you open a file, you simply have to press “F2” (function key 2) to show line numbers in the Vi editor. You can map other keys as you wish to.

Hope that helps.

Stay Digified !!
Sachin Khosla

Share this post: