1

I recently surprised myself wanting to insert blank lines above or below the current line either in normal or insert mode.

I usually add blank lines to my code for better reading or to properly separate blocks of code.

To do this I am using the following in my .vimrc:

" Add empty line above and below the current line
nnoremap <silent> <C-K> mP:pu! _<cr>:']+1<cr>`P
inoremap <silent> <C-K> <esc>mP:pu! _<cr>:']+1<cr>`Pa
nnoremap <silent> <C-J> mP:pu _<cr>:'[-1<cr>`Pa
nnoremap <silent> <C-J> <esc>mP:pu _<cr>:'[-1<cr>`Pa      

The only ugly thing with these mapping is I'm using the marker P just to go back to the previous cursor position. I tried to use `` instead but it doesn't do what I expected.

Perhaps there is a slightly better solution otherwise this snippet works quite well for me.

I am slowly adding new mapping in my .vimrc. I noticed the <C-[a-z]> combinaisons are mostly free and the existing ones are pretty useless (i.e. <C-Q>, <C-H>, <C-M>…). Then I decided to bind them to new useful mapping:

<C-N> New file
<C-S> Save (:update!)
<C-P> CtrlP mixed mode
<C-S-P> CtrlPCmdPalette
<C-B> CtrlP buffers mode
<C-D> <C-C>ciw
…
romainl
  • 186,200
  • 21
  • 280
  • 313
nowox
  • 25,978
  • 39
  • 143
  • 293
  • 3
    Why doesn't `o` or `O` in normal mode do it for you? – merlin2011 May 18 '14 at 19:17
  • For multiples reasons. In insert mode, I need to go back to normal mode then return to insert mode and get my cursor back to its original position I cannot archieve this with a single key stroke. mapping are good because they are accessible from all modes. – nowox May 18 '14 at 20:26

2 Answers2

2

instead of using a named marker, you can use the backtick ' to explicitly add a jump. like:

nnoremap <c-k> m`O<esc>``
nnoremap <c-j> m`o<esc>``
inoremap <c-j> <esc>m`o<esc>``a
inoremap <c-k> <esc>m`O<esc>``a
Kent
  • 189,393
  • 32
  • 233
  • 301
2

If you want a robust mapping that also handles [count], my LineJuggler plugin contains ]<Space> mappings (among others). This particular mapping can also be found in the unimpaired plugin.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324