Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In VIM, why doesn't `db` delete the character under the cursor?

Tags:

vim

I read that b is like the reverse of e i.e. b does what e does but backwards.

So if I hit de on a word and the cursor is on the first letter of the word, it deletes the whole word. That's great!

But if I hit db on a word and the cursor is on the last letter of the word, it deletes the whole word except for the letter that the cursor was on!

I know I could just move over one character when using b but I would like to keep things consistent. And perhaps someone could enlighten me as to why b behaves like this.

like image 692
sq1020 Avatar asked Feb 11 '13 04:02

sq1020


2 Answers

the vim cursor is a pretty disgusting thing and poorly designed, to mimic the block cursor of terminals. the cursor position is actually at the start / left hand side of the block, so if the block is "on" k in bark and you db you're actually deleting from r to b.

fortunately, you can change the cursor to something reasonable

like image 121
Andy Ray Avatar answered Oct 07 '22 16:10

Andy Ray


b is not the opposite of e, as b is an exclusive motion, while e is inclusive. From :help e & :help b:

e Forward to the end of word [count] inclusive. Does not stop in an empty line.

b [count] words backward. exclusive motion.

And from :help exclusive:

A character motion is either inclusive or exclusive. When inclusive, the start and end position of the motion are included in the operation. When exclusive, the last character towards the end of the buffer is not included. Linewise motions always include the start and end position.

like image 21
Andrew Marshall Avatar answered Oct 07 '22 16:10

Andrew Marshall