17

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.

fYre
  • 450
  • 1
  • 5
  • 10

7 Answers7

16

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.

Hajo
  • 176
  • 4
3

Paste without new line

Put this in your .vimrc file:

" Paste yanked line without line breaks before/after cursor position
nnoremap gP i<CR><Esc>PkJxJx
nnoremap gp a<CR><Esc>PkJxJx

Example:

<span style="color:">danger</span>

Now you can type gp to insert #D51B3F after color: while my cursor is at : in Normal mode.

Yank without new line

Use flukus' answer:

" Delete current line without yanking the line breaks
nnoremap dil ^d$

as acronym of delete inner line. This also gets rid of leading indentation.

3

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
Karl Yngve Lervåg
  • 9,494
  • 25
  • 35
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
lcd047
  • 3,700
  • 16
  • 26
2

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.

muru
  • 24,838
  • 8
  • 82
  • 143
flukus
  • 275
  • 1
  • 4
0

Pasting deleted line, without new-line:

Althought It took me quite a while, thankfully I evetually found out how (PS. i'm new to VIM)

  • For VI\VIM and .vimrc, use:
nnoremap dil dd:let @+=matchlist(strtrans(@+),'[ ]*\zs.*\ze\^@')[0]<CR>
  • For NVIM and init.lua (like me), use:
vim.keymap.set('n', 'dil', "dd:let @+=matchlist(strtrans(@+),'[ ]*\\zs.*\\ze\\^@')[0]<CR>")

Demonstration

enter image description here

Regex explained

  • Regex [ ]*\zs.*\ze\^@:
    • [ ]*\zs look (behind) for potential spaces
    • .* and then match anything, only if
    • \ze\^@ at the end there is a ^@

References

Giorgos Xou
  • 101
  • 3
  • This relies on @+ which is a clipboard register and is only written by dd based on the 'clipboard' settings. Instead, you might want to use @" or v:register – D. Ben Knoble Jun 13 '22 at 22:04
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.

muru
  • 24,838
  • 8
  • 82
  • 143