Insert a newline without entering in insert mode, vim
Solution 1
My alternative is using oo
(resp. OO
) to insert a new line under (resp. over) the current through this mapping:
nmap oo o<Esc>k
(resp. nmap OO O<Esc>j
)
Solution 2
Yank an empty line and shift-paste it:
Starting with cursor on empty line:
yy + (shift + p)
"yy" yanks the line, and "shift + p" insert it below, without entering insert mode.
Solution 3
Due to the way that the keyboard input is handled internally, this unfortunately isn't generally possible today. (This particular case should work in GVIM, though.) Some key combinations, like Ctrl + non-alphabetic cannot be mapped, and Ctrl + letter vs. Ctrl + Shift + letter cannot be distinguished. (Unless your terminal sends a distinct termcap code for it, which most don't.) In insert or command-line mode, try typing the key combination. If nothing happens / is inserted, you cannot use that key combination. This also applies to <Tab>
/ <C-I>
, <CR>
/ <C-M>
/ <Esc>
/ <C-[>
etc. (Only exception is <BS>
/ <C-H>
.) This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.
Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals.
But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim 8 major release.
Solution 4
This is what I use:
nmap <CR> :a<CR><CR>.<CR>
I tried nmap <CR> o<Esc>
, but it made UI glitchy as it was switching to insert mode and back.
Solution 5
How about this if you just don't want to press ESC
yypd$
Related videos on Youtube
helq
Updated on February 21, 2021Comments
-
helq about 3 years
I want insert newlines in normal mode in vim using Shift-Enter and Ctrl-Enter. I try some solutions and mixing solutions from Vim Wikia - Insert newline without entering insert mode but Shift-Enter and Ctrl-Enter didn't respond:
" put a new line before or after to this line nnoremap <S-CR> m`o<Esc>`` nnoremap <C-CR> m`O<Esc>`` " reverse J command nnoremap <C-J> vaW<Esc>Bi<CR><Esc>k:s/\s\+$//<CR>$
-
jpincheira almost 10 yearsGreat answer. Just 'o' works and goes to insert mode too. Thanks
-
Canella over 9 yearsThe thing that was bothering me most with the other answers was the shortcut. This one favors my preferences.
-
Mo2 about 9 yearsI did nmap <C-S-J> o<Esc>k since Shift+J deletes newline.
-
MilesF about 7 yearsAnother enhancement is to use
nmap oo m`o<Esc>``
andnmap OO m`O<Esc>``
to preserve cursor position when opening newlines into different indentation levels, as described in these comments. -
MilesF about 7 yearsYou may also want to use a shorter timeout value than the default 1-second to more quickly go into insert mode with plain
o
andO
. I likeset timeoutlen=200
. You'll just need to press the doubleoo
quick enough, and this shrinks the window to execute other non-blocking time-outable commands sequences too. -
DrBeco almost 3 yearssince I have
D
to delete to the end of line, mine would beyypD