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?
2 Answers
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.
-
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
– InKnightNov 19, 2017 at 0:01 -
2
-
@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
andttimeoutlen
are set properly. I don't recommend such a mapping.– statoxNov 20, 2017 at 8:47
@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.
-
x
is such a common command that remapping it systematically doesn't seem like a good idea (let say I mistypelsit
instead oflist
I can't doxp
anymore to invert thes
and thei
). Also if you use it only for visally selected text you should use a mode specific mapping likevnoremap
– statoxNov 22, 2017 at 7:55 -
I hadn't used
xp
before for swapping letter, thank you (had to useXp
to work with my current mapping). Thevnoremap
would reduce how much it changes the underlining functionality. In the end, Vim becomes a custom editor based on the .vimrc you have.– ShadoathNov 22, 2017 at 19:29
ciw
has benefits overce
. Read the text objects help for more.