tags:

views:

134

answers:

4

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.

+4  A: 

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
Benoit
+3  A: 

Try this:

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

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

Amardeep
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
Thanks and I appreciate discovering a feature I never knew I needed until now... ;-)
Amardeep
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
@Benoit: Good suggestion.
Amardeep
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
@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
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.
Somebody still uses you MS-DOS
@Somebody still uses you MS-DOS: Good show!
Amardeep
+2  A: 

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
+2  A: 

Per this duplicate question: http://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]

Fosco
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