Insert a line with single keystroke in VIM Editor

If you are familiar with Vim Editor then you may know that inserting a blank line is quite different from other editors(e.g. gedit) and tedious too.

So to make it more easy and handy I have mapped this operation with minus(-) and plus(+) keys trough map command of vim.

nnoremap + mao<esc>`a
nnoremap - maO<esc>`a

nnoremap means normal non-recursive mapping.

So whenever + or - key is pressed, vim does following:
  • (m) marks current position and saves it in (a) register
  • Insert a blank line below in case of plus sign(o) and above in case of minus sign (O
  • Go back to Normal mode(<esc>) because O or o takes you into insert mode
  • Navigate to previously marked position which was in (a) register

If you want to map a key for only one Vim session temporarily, then you don't need to save the map command in a file. When you quit that Vim instance, the temporary map definition will be lost.

If you want to restore the key maps across Vim instances, you need to save the map definition command in a .vimrc file normally present in $HOME. 

I would recommend to add above two lines in .vimrc and believe me, you will love using that. 


Comments

Popular posts from this blog

MBR partitioning with 'parted' utility

Replace default splash screen in Yocto

Disk Partitioning: MBR vs GPT