Technology
 

Insert newline without entering insert mode

From Vim Tips Wiki

(Redirected from VimTip982)

Tip 982 Previous Next created September 3, 2005 · complexity basic · author Christopher Auer · version 5.7


As you all may know with 'o' or 'O' you can insert a new line before/after 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

[edit] Comments

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>