DavidVII
DavidVII

Reputation: 2283

Create New Line While in Insert Mode

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?

Upvotes: 25

Views: 29441

Answers (4)

Ryan
Ryan

Reputation: 1618

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.

Upvotes: 0

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84343

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.

Upvotes: 36

Vihaan Verma
Vihaan Verma

Reputation: 13143

<ESC> o - To open a line below

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

Upvotes: 31

wedens
wedens

Reputation: 1830

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

Upvotes: 12

Related Questions