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.

8 Answers 8

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

2
  • Does not work for me… it inserts ^M character. Probably it depends on &fileformat.
    – Benoit
    Oct 18, 2010 at 18:38
  • @Benoit, 'fileformat' makes no difference while editing; it's only used for read and write.
    – graywh
    Jul 21, 2011 at 20:05
48

Try this:

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

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

7
  • 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, 2010 at 17:44
  • 2
    Thanks and I appreciate discovering a feature I never knew I needed until now... ;-) Oct 18, 2010 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, 2010 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? Oct 19, 2010 at 12:19
  • 1
    I think I figured it out: remove the blank space between "<CR> <ESC>". The command should be: ":nnoremap <NL> i <CR><ESC>". It's working in my vim. Oct 19, 2010 at 16:36
34

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

R<enter>

No remaps required.

1
  • 9
    Close, but leaves the user in Insert mode, he wants to end up in Normal mode.
    – supermitch
    Jul 8, 2014 at 22:33
31

put cursor in position and...

  r<Enter>
3
  • Not sure why this isn't the top answer since it requires no additional mappings.
    – JC Grubbs
    Jul 9, 2012 at 14:55
  • 9
    Because it replaces character in the current position with a \n
    – caio
    Sep 13, 2012 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, 2017 at 21:19
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 '$'

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.

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]

2
  • 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, 2010 at 17:49
  • 1
    I find it's better to use Vim's key notation (e.g. <CR> and <Esc>) for maps. Also, be more specific regarding map modes.
    – graywh
    Jul 21, 2011 at 20:06
-1

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

1
  • 1
    This is just wrong. I doesn't split, it adds an empty line.
    – trolzen
    Jan 14 at 1:49

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.