23

I would like to use something like Shift + Enter to create a new line in Vim.

So if | is the cursor, here is what I would like to do:

<%= some.code("in here") | %>

Now, press Shift + Enter (or something similar) and get this as output:

<%= some.code("in here") %>
and my new line here |

Is this possible?

chris
  • 4,988
  • 20
  • 36
DavidVII
  • 2,133
  • 2
  • 21
  • 28

4 Answers4

32

Escape to Normal Mode

There are probably a number of ways to do what you want, but one option is to use CTRL-O to escape to normal mode to insert the line. For example CTRL-O o will open a new line below the current line and place your cursor there in insert mode.

If you want to map this rather than use it as a one-off, you can use an imap to set your mnemonic of choice. For example:

:imap \nn <C-O>o

will create an insert-mode mapping for \nn that will do the same thing.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
25

<ESC> o - To open a line below

<ESc> Shift + o - To open a line above.

Vihaan Verma
  • 12,815
  • 19
  • 97
  • 126
12

I use imap <C-o> <esc>o to bind new line on Ctrl+O

wedens
  • 1,782
  • 14
  • 18
  • 10
    I this case you overwrite the default in insert mode which enters in normal mode for one command and switches back. Right? – Alex Shwarc Mar 03 '17 at 13:28
0

As @alex-shwarc points out, <C-o> o (CTRL-o) gets you the behavior you want natively (and conversely <C-o> O to create a new line above and insert into it). <C-o> is really useful, see :help i_CTRL-O.

Ryan Fisher
  • 1,485
  • 1
  • 19
  • 32