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

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.

| improve this answer | |
  • 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. – wanttobegoodatvim Sep 12 '11 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 '11 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.

| improve this answer | |
  • 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. – wanttobegoodatvim Sep 12 '11 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 '11 at 14:33
0

This sounds like what you're after:

Prepare to have your mind blown:

dvb

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: https://www.reddit.com/r/vim/comments/24wbuz/command_to_delete_one_full_word_backwards/chbbkfk/

So, at the end of a line, you can delete two words backwards--including the character under your cursor at the end of the line--with d2vb.

| improve this answer | |

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.