7

My cursor is on the final character of a word in Vim. Without moving the cursor, is there any succinct way to delete the current word and the word before it?

As far as I can tell, the only way to delete the current word is to use diw. Using db leaves the character under the cursor (which happens to be the last character).

3 Answers 3

3

I would do either 2dbx or vbbd. Or v5bd if there were 5 words to delete.

I prefer vbbd as I like to have some visual clue of what I'm going to do and it feels a bit cleaner.

2
  • Wow! I didn't realize that the final character would be highlighted in visual mode, considering it is not highlighted in normal mode. I had always assumed that operators had the same meaning in visual mode. Sep 12, 2011 at 17:53
  • Yes, visual selection always includes the current character. I think it's more logical than d's behaviour to include the current character when going to the right and exclude it when going to the left. This behaviour is quite coherent with y and c's own behaviour but (IMHO) not really with the general idea of "the cursor is ON a letter". This "discrepancy" is another reason why I like to use visual selection.
    – romainl
    Sep 12, 2011 at 19:39
1

Backward moving actions, (like db) take effect from the boundary in front of the cursor.

Probably your easiest way to do this is something like xd2b - that takes care of the character under the cursor first.

Generally I prefer to use Bdw (or wdB) when deleting whole words, as this doesn't leave you with a double space where the word was. Obviously this depends on the context of your actions, and what else you're trying to achieve.

2
  • If you take care of the character under the word first, then the second to last character becomes the first, and it is left behind. d2bxx works (with the xs at the end), but it feels clunky. Sep 12, 2011 at 17:00
  • I'm not sure what you mean. With the cursor on the last character of the word, as per the OP, x deletes the char, and leaves the cursor on the space/punctuation after the word. d2b then works fine. The only times this doesn't work, are if the word is the last one on the line, or the word is only a single character. Like I said in my answer - it depends on context. Also: I've edited the above as wde and edw are wrong (they delete the next word).
    – paulw1128
    Sep 14, 2011 at 14:33
0

d2vb is one option. The added v modifies the motion to also delete the character under the cursor.

 

Details:

Vim makes a distinction between inclusive and exclusive motion. v toggles the "inclusiveness" or "exclusiveness" of a motion. For an example of toggling the opposite direction (inclusive => exclusive), try it with e:

dve

See :help inclusive for an explication. Until now, you thought it was just esoteric nonsense! Didn't you? Didn't you?! (At least, my eyes glazed over whenever I used to read that section in the help... :)

Source.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .