56

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.

Ted
  • 2,211
  • 2
  • 19
  • 27

8 Answers8

52

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...

Andy White
  • 86,444
  • 48
  • 176
  • 211
48

Try this:

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

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

Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50
  • 2
    That is exactly what I was hoping for. I find it odd that this isn't a built in command in vim, but I added the line to my .vimrc file and am happily chugging along. – Ted Oct 18 '10 at 17:44
  • 2
    Thanks and I appreciate discovering a feature I never knew I needed until now... ;-) – Amardeep AC9MF Oct 18 '10 at 17:45
  • 6
    I think it is better to use `:noremap` rather than `:map`, and that it is even better to use `:nnoremap` in this case as the question states *normal mode*. – Benoit Oct 18 '10 at 18:35
  • This doesn't work as expected. When hitting 'i' to enter insert mode, 'enter' to break the line in two, then 'esc' to return to normal mode, the new line is at the same indent as the previous one, and using this trick there's always an extra character. Am I doing something wrong? – Somebody still uses you MS-DOS Oct 19 '10 at 12:19
  • @Somebody still uses you MS-DOS: There does seem to be some kind of formatting action that takes place after the line split. That doesn't happen when you enter the commands manually. I'm not sure what the origin of that is. I'll post the result if I figure it out. – Amardeep AC9MF Oct 19 '10 at 13:06
  • 1
    I think I figured it out: remove the blank space between " ". The command should be: ":nnoremap i ". It's working in my vim. – Somebody still uses you MS-DOS Oct 19 '10 at 16:36
  • Great! Because , the press of [Ctrl]+[Enter] works too. (But watch out the "vim-LaTeX-suite": it is re-map the !) – tron5 Sep 01 '14 at 12:48
34

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

R<enter>

No remaps required.

Martin Lyne
  • 3,157
  • 2
  • 22
  • 28
31

put cursor in position and...

  r<Enter>
SergioAraujo
  • 11,069
  • 3
  • 50
  • 40
  • Not sure why this isn't the top answer since it requires no additional mappings. – JC Grubbs Jul 09 '12 at 14:55
  • 9
    Because it replaces character in the current position with a \n – caio Sep 13 '12 at 14:29
  • also because you might want to break the line on a non white space character, for example at `(|(arg)` which means `arg)` will go on the next line but you lose one of the parens – skamsie Feb 26 '17 at 21:19
3

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.

Carl G
  • 17,394
  • 14
  • 91
  • 115
3

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 '$'

kkress
  • 777
  • 5
  • 6
2

Per this duplicate question: How do I insert a linebreak where the cursor is without entering into insert mode in Vim?

From within vim, type:

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

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

Community
  • 1
  • 1
Fosco
  • 38,138
  • 7
  • 87
  • 101
  • Thanks. I did some searching before posting but I guess I didn't use the right terms. That was a good discussion you linked to, and I feel bad that I posted a duplicate, but I think I got a slightly more elegant answer from Amardeep. – Ted Oct 18 '10 at 17:49
  • 1
    I find it's better to use Vim's key notation (e.g. and ) for maps. Also, be more specific regarding map modes. – graywh Jul 21 '11 at 20:06
-1

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

lehoang
  • 3,577
  • 1
  • 15
  • 16