94

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>$
timss
  • 9,982
  • 4
  • 34
  • 56
helq
  • 1,451
  • 1
  • 9
  • 12
  • What is your `$TERM` (if *nix)? – timss Apr 21 '13 at 18:22
  • `echo $TERM` -> `xterm` Why? – helq Apr 21 '13 at 18:29
  • It may be relevant how the keys are interpreted. Do you use screen or tmux aswell? That being said, I tried a couple of the alternatives, and it would only work for `map o` here. – timss Apr 21 '13 at 18:32
  • well, I don't use screen or tmux with vim. And yes, `map o` work. But, how map `O`? . I'm sorry by my bad english – helq Apr 21 '13 at 18:36

8 Answers8

91

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)

TKrugg
  • 2,255
  • 16
  • 18
  • 6
    Great answer. Just 'o' works and goes to insert mode too. Thanks – jpincheira Jun 10 '14 at 14:00
  • The thing that was bothering me most with the other answers was the shortcut. This one favors my preferences. – Canella Oct 15 '14 at 11:57
  • 1
    I did **nmap ok** since Shift+J deletes newline. – Mo2 Apr 14 '15 at 17:30
  • 11
    Another enhancement is to use ```nmap oo m`o`` ``` and ```nmap OO m`O`` ``` to preserve cursor position when opening newlines into different indentation levels, as described in [these comments](http://vim.wikia.com/wiki/Insert_newline_without_entering_insert_mode). – MilesF Mar 18 '17 at 22:36
  • 10
    You may also want to use a shorter timeout value than the default 1-second to more quickly go into insert mode with plain `o` and `O`. I like `set timeoutlen=200`. You'll just need to press the double `oo` quick enough, and this shrinks the window to execute other non-blocking time-outable commands sequences too. – MilesF Mar 18 '17 at 22:46
27

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.

Rasmus Larsen
  • 5,721
  • 8
  • 47
  • 79
10

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.

Dave Powers
  • 2,051
  • 2
  • 30
  • 34
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
2

I use :s/\n/\r\r/g (subsitute the newline with two newlines, which is the same as "o").

malcolm
  • 33
  • 5
2

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.

Erik B
  • 40,889
  • 25
  • 119
  • 135
2

How about this if you just don't want to press ESC

yypd$
PenutChen
  • 193
  • 8
0

You can copy an empty line to a register and use that. I have built the habit of copying a new line to the n register. You can copy a new line to a register, in this case n using: "ny You can paste the new line anywhere without leaving normal mode like: "np

A. Atal
  • 75
  • 2
  • 7
-3

You can use a hack to do this.

In edit mode, you can use p to paste the current clipboard. Since o adds a newline, you can use o<ESC>ddp to add a new line below the cursor; from there, p will add a new line until you delete something else.

jyn
  • 463
  • 4
  • 16
Diego
  • 9