How to break a line in vim in normal mode?

VimMacvim

Vim Problem Overview


I would like to break a line (at the location of the cursor) in to two lines without leaving normal mode (entering insert or command-line mode). Is this possible?

I currently get to the location I want and hit 'i' to enter insert mode, 'enter' to break the line in two, then 'esc' to return to normal mode.

I am not trying to set a maximum line length or do any syntax or anything like that. I just want to break one line into two lines without leaving normal mode. 'J' joins the line the cursor is on to the line below it, which is handy. I want the opposite -- to break one line into two with a single command.

Vim Solutions


Solution 1 - Vim

Try this:

:nnoremap <NL> i<CR><ESC>

then just press Ctrl-J whenever you want to split a line.

Solution 2 - Vim

I don't know of a single key command, but a lot of times I do "r" then "Enter" to break a line.

"r" replaces the current character under the cursor without going into insert mode. This may not be what you want if you don't want to replace a character...

Solution 3 - Vim

put cursor in position and...

  r<Enter>

Solution 4 - Vim

Similar to other answers but doesn't replace the current character.

R<enter>

No remaps required.

Solution 5 - Vim

As far as I know this isn't possible without entering insert mode. You can however macro it with something like (replace Z with whatever key you want to use)

nmap Z i<cr><esc>k$

basically this maps the key 'Z' to enter insert mode 'i', insert a carriage return '<cr>', leave insert mode '<esc>', go up a line 'k' and finally go to the end of the line '$'

Solution 6 - Vim

Per this duplicate question: https://stackoverflow.com/questions/237383/how-do-i-insert-a-linebreak-where-the-cursor-is-without-entering-into-insert-mode

From within vim, type:

:map g i[Ctrl+V][Enter][Ctrl+V][Esc][Enter]

This maps the G key to macro I [Enter] [Escape]

Solution 7 - Vim

You can use recording.

  1. Place your cursor where you would like to insert a line break.
  2. Type qa to start recording into register a (you can use another register other than a if you want.)
  3. Then type i (switch to insert mode), Return (insert newline), escape (exit insert mode), q (ends recording.)

Now you can invoke this sequence of keys by typing @a (where a is the register number you used when you started the recording), just keep moving the cursor where you want to insert a newline and type @a.

Solution 8 - Vim

In normal mode, Press the character 'O' then 'Esc'. No mapping needed.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTedView Question on Stackoverflow
Solution 1 - VimAmardeep AC9MFView Answer on Stackoverflow
Solution 2 - VimAndy WhiteView Answer on Stackoverflow
Solution 3 - VimSergioAraujoView Answer on Stackoverflow
Solution 4 - VimMartin LyneView Answer on Stackoverflow
Solution 5 - VimkkressView Answer on Stackoverflow
Solution 6 - VimFoscoView Answer on Stackoverflow
Solution 7 - VimCarl GView Answer on Stackoverflow
Solution 8 - VimlehoangView Answer on Stackoverflow