97

Possible Duplicate:
How do I insert a linebreak where the cursor is without entering into insert mode in Vim?

In vim, J joins the next line to the current line. Is there a similar one-key (or relatively short) command to split a line at a given cursor position? I know it be done with a simple macro, but it seems like if the J-command exists there should be a similar function. I've tried searching for it, but can't seem to find an answer.

Community
  • 1
  • 1
dburke
  • 1,236
  • 2
  • 11
  • 15
  • 2
    Does this help? [How do I insert a linebreak where the cursor is without entering into insert mode in Vim?](http://stackoverflow.com/questions/237383/how-do-i-insert-a-linebreak-where-the-cursor-is-without-entering-into-insert-mode) – bedwyr Mar 09 '09 at 02:38
  • No, I as really looking to see if maybe there was some base command I had overlooked or been unable to find that someone else here might know. – dburke Mar 09 '09 at 02:47

7 Answers7

187

rEnter while on whitespace will do it. That's two keystrokes.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125
  • 12
    Close, but no cigar. I'm often wanting to split between two adjacent html tags with no space between. – dburke Mar 09 '09 at 02:45
  • 3
    @dburke In that case, put the `s` mapping here (http://vim.wikia.com/wiki/Insert_a_single_character) in your `.vimrc` and use ``. – Kyle Strand Jun 02 '14 at 20:25
  • `i` `Enter` will split the line between two adjacent html tags with no space between. @dburke – dcx86 Apr 04 '19 at 10:26
40

I don't think that there is a single key command for this. The best you can do with stock vim is probably i Enter Esc.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
Brian Pellin
  • 2,859
  • 3
  • 23
  • 14
36

My solution was to remap the K key since I never use the default action (look up the word under cursor with "man"), and my previous editor used Alt+j and Alt+k to join and split lines, respectively.

:nnoremap K i<CR><Esc>

This rolls those three annoying keystrokes into one.

There's probably a more sophisticated way to also eliminate any trailing whitespace, but I prefer to strip all trailing whitespace on write.

Steve
  • 6,618
  • 3
  • 44
  • 42
  • 4
    This is great! Usually accidentally pressing `K` is disastrous for me because of the stupid `man` page functionality, but now I get to replace that with something that's actually useful. – Explosion Pills Mar 02 '13 at 17:50
  • 3
    This is a good redefine, but the default behavior of `K` shouldn't be thought as strictly `man` only. Many plugins redefine `K` as to lookup the docs in the context language. In PHP, for instance, `K` would take me to php.net documentations... – mike3996 Apr 24 '13 at 08:46
23

No. I've now read enough answers to conclude that there is no such command.

Easy answer: Pressing 'Enter' while in insert will do it; but you're right, there oughtta be a key for it in command mode. I've wondered, too.

Since everyone has a favorite workaround, I will share mine. The assumption is that I will do anything to avoid having to reach for the Esc key.

ylprX ... where 'X' is the inserted character, which can even be a newline.

So, 'yl' is yank on char to the right, 'p' = paste the char, 'r' is replace that char; then you just type the new char. That's how much I hate using Escape.

(That was 'l', as in "move right", BTW)

gbarry
  • 10,352
  • 6
  • 33
  • 43
11

Old thread, but I dont use "K" for the man page lookup or whatever magic it does. So I have this mapping in my .vimrc:

map K i<Enter><Esc>

I figured since "J" is join, "K" can be krack or something. :)

Johnny Sparks
  • 360
  • 2
  • 7
10

You can split lines if you can create a regular expression for the location to add the split. For example if you want to split the lines at each semicolon, you can use the following substitution:

%s/;/^v^m/g

to great effect

0

Jed's answer is most useful. I would like to add that I needed the "control-V-alternative", i.e. control-Q: %s/;/^q^m/g