LEARNING GENTLEMAN

Vim tips series – useful mappings (1)

[In these series I share some of my favourite Vim editor tips. This is not a tutorial for beginniner, it assumes you have basic knowledge of how Vim editor works. In fact, I like to consider this series as advanced Vim topics. ]

1) Switch to Normal mode faster

Switching between insert and normal mode in Vim is done quite frequently. In fact, making this action faster, will in the long run make you much more productive.

Anyone just starting to use Vim will find themselves pressing ESC key to move to Normal mode. This is very inefficient as reaching out to ESC key is a tedious action. Do yourself a favour and map switching to normal mode to “jj”. Opet your .vimrc file and add the following line:

imap jj <ESC>

Now you can move from insert to normal mode even faster by simply typing jj.

2) Center your screen when cycling through the search results

If you press “*” while cursor is above a word, it will mark it. By pressin “n” you can cycle through all the same words in file. What I find mildly irritating is that it will always position search result at the bottom of the screen. If you add the following to your .vimrc file, search pattern will always be at the center of the screen:

nnomap n nzz

Very handy.

3) Leave a mark before jumping to beginning of the file, so you can come back

Using marks in Vim is a very powerful feature, yet not many people use it. Here is an interesting example how useful this simple feature is. I often myself jumping to the beginning of the file by typing “gg”, usually to check header or update the description. In most cases, I want to go back to a place before I jumped. If I add mark named “j” before jumping, by doing “mj”, I can go back with “`j” – too big of a hassle.

You can however automatically add mark before jumping by adding the following to your .vimrc:

nnoremap gg mjgg

Now when you jump, marker is automatically placed, and you can go back with “`j”.

4) Remap redo function.

By default, undo function is executed by typing “u”. In that sense I find it very useful to map redo to “U”. This is simply done by adding the following to your .vimrc file:

nnoremap U <C-R>

5) Quickly save from normal mode

One of my favourites. There are a few ways to save a file in Vim, but I like the following one the most. In Vim terms, “Leader” is a name for the following character: “\”. You can quickly map typical save steps in the following way:

nnoremap <leader>s :w<cr>

“Leader” is not used in Vim and so is very useful to be used in differnet maps, since it’s easily accessible.

Conclusion

There are countless tricks you can do with Vim editor, this is why many like is (and well, probably why many hate it, as it requires a bit of different mindset, compared to typical editors). I hope you will find them useful!
Share in the comments your own favourite Vim tips!

You may also like

Leadership
Milos

Here’s How To Beat Fear of Interviews

John is a verification engineer with seven years of experience. He has successfully completed multiple projects in his career and has also led a team of five people. However, despite having a clear track record of being good at his

Read More »
Personal Development
Milos

My year (2022) in numbers

Peter Drucker, the father of modern management, said, “What gets measured gets improved.“   I admit I am a massive fan of all kinds of metrics, and I love tracking my habits. This process motivates me and pushes me toward

Read More »
Join my Mailing list!