Inserting a blank line in vim?

25

11

I quite often find I have a need to insert a blank line either below or above the current line when editing in vim. o and O will do this, but they subsequently switch into insert mode, which is annoying. Is there any built-in command to do this which will remain in normal mode?

Andrew Ferrier

Posted 2013-06-13T13:18:20.040

Reputation: 1 604

Answers

10

Both Tim Pope's unimpaired plugin as well as my own LineJuggler plugin provide [<Space> and ]<Space> mappings to add [count] blank lines above / below the current line.

Basically, it boils down to this:

nnoremap <silent> ]<Space> :<C-u>put =repeat(nr2char(10),v:count)<Bar>execute "'[-1"<CR>
nnoremap <silent> [<Space> :<C-u>put!=repeat(nr2char(10),v:count)<Bar>execute "']+1"<CR>

Ingo Karkat

Posted 2013-06-13T13:18:20.040

Reputation: 19 513

I now use this, so I've marked this as the "correct" solution; but all the answers here are great. – Andrew Ferrier – 2014-10-12T09:31:12.023

Great tip, although I preferred to map <Space>o and <Space>O instead. – simlev – 2017-03-28T14:30:57.433

26

I've been using these

map <Enter> o<ESC>
map <S-Enter> O<ESC>

in my .vimrc for years.

Press Enter to insert a blank line below current, Shift + Enter to insert it above.

Mr Shunz

Posted 2013-06-13T13:18:20.040

Reputation: 2 037

So simple! This should be the accepted answer! – Sheharyar – 2015-10-26T14:55:18.680

If you don't want the cursor to move when you hit Enter, put k at the end of the first map and j at the end of the second map. – zondo – 2016-11-24T02:38:05.720

1

Note that mapping Shift-Enter only works with the GUI version of vim, not the terminal version. http://stackoverflow.com/questions/16359878/vim-how-to-map-shift-enter

– ishmael – 2016-12-26T18:39:47.957

I am marking this correct as it's the simplest solution, although all these answers are good. – Andrew Ferrier – 2013-06-19T13:07:08.027

7

Yet another way to insert lines above or below:

nnoremap <Enter> :call append(line('.'), '')<CR>
nnoremap <S-Enter> :call append(line('.')-1, '')<CR>

Note that the solution from romainl and Mr Shunz will move the cursor to the newly inserted line, whereas this and also the one from Ingo Karkat will keep the cursor at the same spot.

taketwo

Posted 2013-06-13T13:18:20.040

Reputation: 500

1

No, there's no built-in command for that.

These mappings do what you want:

nnoremap <leader>o o<Esc>
nnoremap <leader>O O<Esc>

romainl

Posted 2013-06-13T13:18:20.040

Reputation: 19 227