13

An example (The ^ is the cursor position):

Before:

a bc def ghi
j k l mn o p q rs
      ^

After:

a bc def ghimn o p q rs
            ^

or at least:

a bc def ghimn o p q rs
           ^

I know I can use d0i<BS>, but I want a faster way to do this and I want to remain in the command mode. I am a beginner in vim and I think that maybe the answer is a modification of kA.

Thank you very much! :-)

1
  • 1
    If this is something you do frequently, you should consider writing your own mapping for it, maybe something like <Leader>dk. Oct 26, 2015 at 23:31

4 Answers 4

14

Try d0kJx

Deletes backwards to the beginning of the line, moves up, then joins the two lines and then removes the space in between the two joined lines.

You don't have to leave normal mode with this.

1
  • 2
    This is the only answer so far that (1) doesn't alter the search history or change the current highlighting and (2) doesn't delete the m. Oct 26, 2015 at 23:32
13

Well, you can combine the "backward search" motion and the delete operator:

d?$<Enter>
2
  • In .vimrc I have this: set hlsearch and d?$<Enter> does what I asked but it also highlights all the line endings in the file so I have to type :noh after that to remove the highlight. Is there a way to avoid this? Thank you! :-) Oct 26, 2015 at 16:30
  • 1
    There probably is, what I do is simply "overload" <C-l> in normal mode: nnoremap <silent><C-l> :nohlsearch<cr>:set nolist<cr>:redraw!<cr>, then use it when needed (i.e. in cases such as the above).
    – VanLaser
    Oct 26, 2015 at 16:34
4

With visual mode: hvk$d

Explanation
h - Move one to the left
v - Start visual mode
k$ - Move to the end of the previous line
d - Delete selection

2
  • With cursor on m, maybe hvk$d ? (to avoid selecting it)
    – VanLaser
    Oct 26, 2015 at 23:08
  • 1
    Ah, yes. Updating now. Oct 27, 2015 at 6:57
0

In tonight's episode of Creating custom motions for fun and profit : "From cursor to end of {count} previous line".

onoremap <expr> g- '<Cmd>normal v' .. v:count1 .. (getpos('.')[2] == 1 ? '-$o-$' : '-$oh')  .. '<CR>'
xnoremap g- -$

Usage:

dg-
d2g-
v3g-d

How it works:

  • :omap creates an operator-pending mapping,
  • that is how you create new motions,
  • <expr> makes it possible to evaluate expressions and change the behavior of the mapping at runtime, this is extremely useful,
  • the RHS of the mapping essentially makes a visual selection that starts at the cursor and extends to the end of the {count}th line above,
  • the area is slightly modified to work as expected (by me, at least) depending on the position of the cursor on the line,
  • v is not an operator so operator-pending mappings won't work, we need a visual mode mapping,
  • the mapping is simpler because visual mode is inclusive and we don't need special cases.

Adjust to taste.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.