LEARNING GENTLEMAN

Vim tips series – useful mappings (2)

[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. In case you missed the first post, you can find it here]

1) Use c for accessing system clipboard

If you want to copy something from vim to your clipboard, special register “+ has to be used. So for example, you would select a part of text and then type “+y. I find this very tedious and to be honest, I often forget it. In order to make this process easier, add the following to your .gvimrc file:

nnoremap <leader>c “+

Now you can just select piece of text and while in normal mode press \cy and voala, text is copied to your clipboard.

2) Select the whole file easily

If you want to select the whole file – what in windows we typicaly do by pressing ctrl-a, in vim you have to type ggVG. If you want to mimic the windows behavior, add the following to your .gvimrc file:

nnoremap <C-a> ggVG<end>

Now you can just press ctrl-a, and there you have it, whole file selected.

3) Automatically add enclosed brackets

If you want to spare yourself from explicitelly typing “)” every time you type “(“, add the following to your .gvimrc file:

inoremap ( ()<ESC>i

This will automatically produce () and put vim in insert mode and cursor will be between the brackets, where you wanted to type anyway.

4) Automatically produce {}; when you type {

Depending on the programming language you are using, your related statements will be enclosed in some variant of “begin-end” clause. If for example that is

{

};

as it is in e/Specman, you can save yourself some time by adding the following to your .gvimrc:

inoremap {<cr> {<cr>};<ESC>kA<CR>

Once you press { followed by <enter>, it will add }; automatically.

5) Quickly inspect buffers and open desired one with one click

If you want to inspect buffers and then open specific one, you have to type:

:ls
enter
b2
If for instance you want to open buffer 2.

You can speed this up by adding the following to your .gvimrc:

nnoremap gb :ls<CR>:b

In this way, you only type gb while in normal mode, followed by a number for the desired buffer. For example: gb 2.

Conclusion

I hope you like these quick and simple vim tips. I encourage you to play with them and discover your own shortcuts and maps, that will make you even more productive.

You may also like

Join my Mailing list!