2

Right now I press ce to replace a word. However, this overrides anything that was in my register before. How can I replace a word and go into insert mode after without replacing what was in my register previously?

1
  • 1
    Side note: ciw has benefits over ce. Read the text objects help for more.
    – D. Ben Knoble
    Nov 19 '17 at 5:46
6

You need to use the black hole register (see :h quote_):

When writing to this register, nothing happens. This can be used to delete text without affecting the normal registers. When reading from this register, nothing is returned.

Simply do "_ce so that the deleted text will be put into the black hole register and not into your unnamed register.

3
  • Thanks a lot, just as a follow up if I wanted to remap ce to do this by default would this work: nnoremap ce "_ce
    – InKnight
    Nov 19 '17 at 0:01
  • 2
    Why not try it yourself and see?
    – B Layer
    Nov 19 '17 at 6:32
  • @InKnight (BLayer makes a really good point) Yes it should work but I think that would be extremely inconvenient and you'd need to make sure that ttimeout and ttimeoutlen are set properly. I don't recommend such a mapping.
    – statox
    Nov 20 '17 at 8:47
0

@statox is spot on with the Black hole register.

In fact I use this so much I remapped to my x key in my .vimrc file: noremap x "_x This allows me to visual select then x it away while maintaining my last yanked text.

If you want to swap text from one place to another check out tommcdo/vim-exchange.

2
  • x is such a common command that remapping it systematically doesn't seem like a good idea (let say I mistype lsit instead of list I can't do xp anymore to invert the s and the i). Also if you use it only for visally selected text you should use a mode specific mapping like vnoremap
    – statox
    Nov 22 '17 at 7:55
  • I hadn't used xp before for swapping letter, thank you (had to use Xp to work with my current mapping). The vnoremap would reduce how much it changes the underlining functionality. In the end, Vim becomes a custom editor based on the .vimrc you have.
    – Shadoath
    Nov 22 '17 at 19:29

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.