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

4

17

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?

Stibnite answered 27/5, 2015 at 20:12 Comment(2)
In normal mode, put your cursor anywhere on var myVar = line and type J.Countrydance
@amphetamachine, that's the correct answer. Post it as answer.Disk
C
31

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.

Countrydance answered 27/5, 2015 at 20:19 Comment(0)
G
1

kJ will do what you want and is probably what you should be using, however if you want to do exactly what you've asked for Delete backwards from cursor to the end of the previous line then you can do the following:

:set virtualedit+=onemore
^  " go to the start of the line
d?$<cr>

?$<cr> is a movement that goes to the end of the previous line. :set virtualedit+=onemore allows the cursor to move just past the end of the line, without which we would end up deleting the last character of the line, which in the example you have given would be the trailing space.

You could then create a mapping to do this (:nohl just clears the search highlighting):

:nnoremap <leader>J ^d?$<cr>:nohl<cr>

Although a simpler mapping to achieve the same thing would be:

:nnoremap <leader>J kJ
Gavin answered 27/5, 2015 at 21:12 Comment(0)
R
1

Also,

:set backspace=indent,eol,start

The backspace option determines the behavior of pressing the backspace key (). By default, Vim’s backspace option is set to an empty list. There are three values that can be added that each independently alter the behavior of the backspace key. These are indent, eol, and start.

When indent is included, you can backspace over indentation from autoindent. Without it, Vim will not allow you to backspace over indentation.

When eol is included, you can backspace over an end of line (eol) character. If the cursor is at the first position of a line and you hit backspace, it will essentially be joined with the line above it. Without eol, this won’t happen.

When start is included, you can backspace past the position where you started Insert mode. Without start, you can enter Insert mode, type a bit, and then when backspacing, only delete back as far as the start of Insert mode.

The backspace default is absurd, you are going to want to add all of the above to your Vim settings.

See :h 'backspace' for more details.

Ridenhour answered 27/9, 2022 at 19:33 Comment(0)
L
0

Just to refine the excellent answer by Brett, at least for my case, I needed to use the following command in my config:

inoremap <silent><nowait> <C-d> <Esc>kJ<Delete>i

I wanted this command to work specifically in Insert mode and stay in Insert mode (mapping to Ctrl+d combination that doesn't interfere with writing code). Also, for me the J command adds an extra space when joining lines right at the cursor, so I added <Delete> to remove it. <Esc> and i just exit and enter back Insert mode correspondingly.

Landgrave answered 31/7 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.