vimvi

Delete backwards from cursor to the end of the previous line in Vim?


Say I want to edit the following line:

var myVar = 
    "I am a string!";

So that it looks like this:

var myVar = "I am a string!";

Is there a movement that goes to the end of the previous line?


Solution

  • What you want is the line join command, J.

    In normal mode, put your cursor anywhere on the var myVar = line and type J (capital j).

    Direction-wise motions work with J - 5J indents 5 lines below the cursor, etc. You can also select a range of lines using visual mode (v to start visual mode selection) and join them all into one using J.

    See the Vim documentation for this key.

    The &joinspaces option affects this behavior as well. When it's "on" (set joinspaces or set js) it adds two spaces after a sentence-ending mark (i.e. '.', '?', or '!') when joining lines. set nojoinspaces or set nojs to turn that off and insert only one space.