10

Many times in vim I would like to delete a line completely, so I use dd. However, I then need to paste that line into some other position within a line, but this inserts a newline before pasting, therefore, making it very difficult for me to get the desired result. For example,

while( pasteInHere )
{
    cin >> n; // Delete this line completely with dd
}

when I do this with the above code I get:

while( pasteInHere )
cin >> n; // Delete this line completely with dd
{
}

which is very far from the result I want...how can I suppress this newline behaviour, or use another method that does it very efficiently? I don't think d$ is good because I not only have to go to beginning of line, but if I want to delete the empty line too I need to delete it in another register, I feel like there should be an easier way! Thanks.

  • 1
    possible duplicate of Can I delete an entire line, except the line break? – EvergreenTree Jul 14 '15 at 20:37
  • 1
    it is not a duplicate, as I said I want a more efficient way then going to beginning of line, also I actually want to remove the complete line with the newline character....not just the contents of the line – fYre Jul 14 '15 at 20:42
  • Oh, I do see what you mean now. It just wasn't very clear to me what you were trying to accomplish. – EvergreenTree Jul 14 '15 at 20:48
  • 0D delete line except line break. And than gJ to join lines. Now you can put the text without break line and no empty line. – Alex Kroll Jul 14 '15 at 22:02
8

You can go anywhere in the line above the line you want to delete, then press JD and paste it with p at the desired point.

  • J joins the two lines and moves you at the start of the text you wanted to delete. This deletes a new-line character and the indentation of the line you want to move.
  • D deletes from the current cursor position to the end of the line but preserves the new-line character.

Hint: You can use :pu if you deleted with D or yanked with y$ and you want to paste with a new line.

3

Not really simpler, but slightly more efficient, and you can also create normal-mode maps for both sequence:

  • yanking: go to the line you want to move, then ^D"_dd
  • pasting: go to the word you want to replace, then viwp
2

You can achieve something that works quite well with an imap. Add the following to your vimrc:

inoremap <c-a> <c-r>1<esc>k$Jxi

This will create a mapping for CTRL-a that will paste the last deleted text (with dd or similar). If you prefer, you may of course use a different key for the mapping.

References:

:h i_CTRL-R
:h quote1
:h J
1

This comes up for me a bit too, never thought to optimize it before but this mapping works:

nmap dil ^d$

dil is "delete in line", similar to diw, di", etc.

0

You can select the text you want to copy by pressing v and then take it into buffer by pressing y and then paste it anywhere by p. It don't inserts any new line.

  • True, but this still leaves a blank line. – muru Jul 21 '15 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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