Vim substitute till end of line in normal mode

Viewed 141

I have some text yanked using yiw. Now, on another line, I want to replace text from current cursor position until end of line, so I try v$p. However, this also deletes the newline at the end of the line which I don't want to happen. How do I address this?

2 Answers

You can:

  • adjust the visual area before putting:

    v$hp
    
  • use the :help g_ motion if you don't have trailing space:

    vg_p
    

You can delete into the null register and then paste with "_Dp

Alternatively, if you want to save the text you're deleting, you can delete in the normal buffer, and past from the 0 buffer with D"0p

I find register keys clunky, so I have maps in my .vimrc for them:

" leader means "another option key like cmd or meta, but one you get to choose"
let mapleader = "\<Space>"
" get the yank register, not the thing you just deleted
nnoremap <Leader>p "0p
nnoremap <Leader>P "0P
" don't copy the thing you're gonna delete into the delete register
nnoremap <Leader>d "_d
nnoremap <Leader>D "_D

With these binds, it becomes Dp or D p depending on whether you want to keep the deleted text.


If you don't want to mess with registers at all, there's always the simple PlD.

Related