Vim: delete without copy

Fang Jin
2 min readJan 13, 2024
Photo by Thomas Park on Unsplash

I’m a fan of vim, always providing me a boost of efficiency when I need it. But I have to admit, sometime it gets me pretty frustrated on things that is quite obvious to do in other basic editors, such as delete.

If you hit x , you’d expect to remove a character under your cursor.

abcdefghijklmn
-
x

Pretty straight forward. But behind the scene, it also does something extra, by putting the removed character into the buffer. So next time when you paste with p, it behaves as if you have pressed Ctrl-C on your keyboard. Some might find this useful, but some can find this extremely annoying.

Lately I have been doing a lot of editing such as the following.

···Q····
······Q·
··Q·····
·······Q
·Q······
····Q···
Q·······
·····Q··

I need to copy and paste while deleting. But I really don’t want to paste the deleted character, instead I want to keep pasting the same character, such as . in my case. The delete bothered me before, but this really drove me nuts.

Luckily it also bothered other people. So I searched around and the solution actually isn’t complicated at all.

Just put a line in .vimrc .

nnoremap x "_x

Now, with this change, when you hit x next time, it only does delete with no copy attached.

If you happen to use Visual Studio code or other editor with vim embedded, you can turn on a flag to read this setting file.

Now your favorite editor supports this change as well.

I’m sure you can improvise from this point on, for instance, you probably don’t want d to copy either. When you delete multiple lines one by one with dd, you still want to paste something you had in mind earlier. In that case, you can fix it by adding another line in .vimrc .

nnoremap d "_d

Hope this helps.

--

--

Fang Jin

#OpenToWork Front-end Engineer, book author of “Designing React Hooks the Right Way” sold at Amazon.