Vim split line command

Vim

Vim Problem Overview


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

Vim Solutions


Solution 1 - Vim

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

Solution 2 - Vim

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.

Solution 3 - Vim

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.

Solution 4 - Vim

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)

Solution 5 - Vim

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

Solution 6 - Vim

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

Solution 7 - Vim

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

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
QuestiondburkeView Question on Stackoverflow
Solution 1 - VimMichael KristofikView Answer on Stackoverflow
Solution 2 - VimBrian PellinView Answer on Stackoverflow
Solution 3 - VimSteveView Answer on Stackoverflow
Solution 4 - VimgbarryView Answer on Stackoverflow
Solution 5 - VimJohnny SparksView Answer on Stackoverflow
Solution 6 - Vimuser162267View Answer on Stackoverflow
Solution 7 - Vimuser269423View Answer on Stackoverflow