Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create New Line While in Insert Mode

Tags:

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?

like image 543
DavidVII Avatar asked Aug 09 '13 01:08

DavidVII


People also ask

How do you move in Insert mode?

If you want to type in text, you need to be in Insert mode. To move from Command mode to Insert mode, press "i" (no quotes). To move from Insert mode to Command mode, press "ESC" (the Escape key). NOTE: If your terminal doesn't have an ESC key, or the ESC key doesn't work, use Ctrl-[ instead.

How do you enter Insert mode and go to the beginning of the line?

Home in insert mode. Esc + i to exit insert mode and enter it again, effectively going to the beginning of line.

How do I Insert a line break in Vim?

Using search and replace In the substitute command, the find pattern is empty, so the last search is used; in the replacement, \r inserts a newline and & inserts the search hit (see search and replace). Use :%s//\r&/g if you want to replace all occurrences in all lines.

How do we make a new line under the current line?

If you want a new line at the top of the current line then press CTRL + SHIFT + ENTER. If you want a new line at the bottom of the current line then press CTRL+ ENTER.


1 Answers

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.

like image 58
Todd A. Jacobs Avatar answered Oct 03 '22 23:10

Todd A. Jacobs