Vim Tips Wiki

Insert newline without entering insert mode

Revision as of 08:26, June 27, 2013 by Sias-vim (Talk | contribs)

1,619pages on
this wiki
Tip 982 Printable Monobook Previous Next

created 2005 · complexity basic · author Christopher Auer · version 5.7

As you all may know with 'o' or 'O' you can insert a new line after/before the current line. But both commands enter the insert mode, which may sometimes not what you want. I put this in my vimrc-file to insert a new-line after the current line by pressing Enter (Shift-Enter for inserting a line before the current line):

map <S-Enter> O<Esc>
map <CR> o<Esc>

If you want to stay in the line where you have been before use the following maps:

map <S-Enter> O<Esc>j
map <CR> o<Esc>k

See also


It's also a nice idea to map something like

nnoremap <C-J> a<CR><Esc>k$

because it's the opposite of Shift-J. A more sophisticated solution would be

nnoremap <C-J> ciW<CR><Esc>:if match( @", "^\\s*$") < 0<Bar>exec "norm P-$diw+"<Bar>endif<CR>

For whatever reason, I was unable to get the suggest mapping with <S-Enter> and <CR> to work properly (both ended up inserting below). So I did:

map <F8> o<Esc>
map <F9> O<Esc>

Which works fine for me.

<S-Enter> and <CR> will work separately in gvim but not in urxvt. See

Since I have inoremap jj <Esc> in my .vimrc, it take not too much to type ojj or Ojj.

To add a blank line below or above and keep the cursor in place, I think this is easiest:

nnoremap <C-J> m`o<Esc>``
nnoremap <C-K> m`O<Esc>``

It uses the jump list to remember the cursor position, so it does clutter up that list. But I don't mind. You could use any other mark, like so (uses mark a):

nnoremap <C-J> mao<Esc>`a
nnoremap <C-K> maO<Esc>`a

This mapping interferes with command execution in the command-line window. The command-line window can operate in normal or insert mode and in both <CR> executes the command from current line. Using 'nmap' instead of 'map' in the mapping declaration partially fixes the problem. Then, to execute the command, you must enter the insert mode and press <CR>. While this is a feasible workaround, I prefer to dispense with this mapping and type 'oii' to insert a new line ('ii' being my map for <Esc>).

Around Wikia's network

Random Wiki