287

I'm now switching to Vim from TextMate. I found ^+W in INSERT mode very useful. However, I'd like to delete not only the word before cursor, but the word after or around cursor as well.

I did some googling, but the only thing I can find was ^+W to delete word BEFORE cursor.

0

16 Answers 16

694

Normal mode:

daw : delete the word under the cursor caw : delete the word under the cursor and put you in insert mode 
Sign up to request clarification or add additional context in comments.

5 Comments

It's helpful for me to think of it as daw : delete a word and caw : change a word.
Probably worth mentioning that aw is one example of object selection. You can try object selections in visual mode to see what effect they have: vimdoc.sourceforge.net/htmldoc/motion.html#object-select
Why do I get E492: Not an editor command: caw?
@SebastianNielsen Just stumbled upon this while searching around, not sure if you found your answer, but it's because it's not a command, it's a shortcut. Just type caw or daw while in Normal mode, don't enter it with a : like a command.
What if I have "something_like_this" and I position cursor on "i" in "like". Any chance to delete only the "like" word?
131

I think it's just daw

daw - delete a word 

4 Comments

I know that a is a command for Append text after the cursor [count] times. but how does daw work?
@REACHUS the initial character d dictates you'll be 'deleting'. The 'a' is a kind of parameter, specifying that the deletion is to include a delimiter. Finally the 'w', another parameter -if you will- specifies that a word unit will be deleted.
what's the difference between dw and daw ?
@AK_ Late response, but if you're in the middle of a word, dw will delete from the cursor to the end of the word, while daw will delete the entire word along with a space. diw will delete the entire word without touching whitespace around it.
86

What you should do is create an imap of a certain key to a series of commands, in this case the commands will drop you into normal mode, delete the current word and then put you back in insert:

:imap <C-d> <C-[>diwi 

2 Comments

":h diw" for a lot of useful shrtcts!
Bonus bonus points for using <C-o>. It executes one command in Normal mode and jumps straight back into Insert, so something like imap <C-d> <C-o>diw works too. It also doesn't create an undo point, which <Esc> pretty much always does.
86

The below works for Normal mode: I agree with Dan Olson's answer that you should probably be in normal mode for most deletions. More details below.

If the cursor is inside the word:
diw to delete in the word (doesn't include spaces)
daw to delete around the word (includes spaces before the next word).

If the cursor is at the start of the word, just press dw.

This can be multiplied by adding the usual numbers for movement, e.g. 2w to move forward 2 words, so d2w deletes two words.

Insert Mode ^w
The idea of using hjkl for movement in Vim is that it's more productive to keep your hands on the home row. At the end of a word ^w works great to quickly delete the word. If you've gone into insert mode, entered some text and used the arrow keys to end up in the middle of the word you've gone against the home-row philosophy.
If you're in normal mode and want to change the word you can simply use the c (change) rather than d (delete) if you'd like to completely change the word, and re-enter insert mode without having to press i to get back to typing.

Comments

54

Do you mean like?

dw 

4 Comments

Late to the party, but dw will delete from the current position to the end of the word, to delete the whole word from the beginning use diw or daw
dw won't delete past a hyphen or other punctuation.
Indeed, using dW will.
*Note: This will overwrite buffer/last yank. This is more of a 'Cut' operation, not only 'Delete'.
33

To delete all characters between two whitespaces, in normal mode:

daW 

To delete just one word:

daw 

Comments

21

It doesn't look like there's any built-in way to do it in insert mode, which was the question. Some of the other answers are correct for normal mode, as well as pointing out that a custom mapping could be created to add the functionality in insert mode.

Honestly, you should probably do most of your deleting in normal mode. ^W is neat to know about but I'm not sure I can think of a situation where I'd rather do it than esc to go into normal mode and have the more powerful deletion commands at my disposal.

Vim is very different from a number of other editors (including TextMate) in this way. If you're using it productively, you'll probably find that you don't spend very much time in insert mode.

3 Comments

I use ^w a lot, mostly when I mistype a word - instead of going back and adding the missing letter/etc, I just delete the whole thing and start again.. Going to normal-mode just to delete one word seems like a waste of a few keystrokes.. For example, "example wrd[^w]word" is less keystrokes than "example wrd[^c][dw][i]word"
dbr: He wasn't asking to do the basic ^W behaviour though, that vim supports. He was asking to delete all of a word while in the middle while in insert mode, which is a kinda weird situation for vim and so there's no command to do it. You could always bind ^Q to <Esc>ciw if you really wany the functionality. Oh, also the [i] in [^c][dw][i] is redundant, you can use [^c][ciw]
"It doesn't look like there's a way ..." - well there are countless ways to do it - there is just no built-in way, afaik. While I fully agree with Dan's statement on insert mode vs. normal mode, you could of course simply define an insert mode mapping, see ":help imap" that does whatever you want to achieve if you really need the functionality in insert mode.
19

Since there are so many ways to delete a word, let's illustrate them.

Assuming you edit:

foo-bar quux 

and invoke a command while the cursor is on the 'a' in 'bar':

foo-bquux # dw: letters then spaces right of cursor foo-quux # daw: letters on both sides of cursor then spaces on the right foo- quux # diw: letters on both sides of cursor foo-bquux # dW: non-whitespace then spaces right of cursor quux # daW: non-whitespace on both sides of cursor then spaces on the right quux # diW: non-whitespace on both sides of cursor 

Comments

14

To delete the entire word your cursor is on use diw
To delete the entire word your cursor is on and to put you in insert mode use ciw
if you dont want to delete the entire word but delete from where you are the dw/cw

Comments

6

Insert mode has no such command, unfortunately. In VIM, to delete the whole word under the cursor you may type viwd in NORMAL mode. Which means "Visual-block Inner Word Delete". Use an upper case W to include punctuation.

Comments

3

The accepted answer fails when trying to repeat deleting words, try this solution instead:

" delete current word, insert and normal modes inoremap <C-BS> <C-O>b<C-O>dw noremap <C-BS> bdw 

It maps CTRL-BackSpace, also working in normal mode.

Comments

2

For deleting a certain amount of characters before the cursor, you can use X. For deleting a certain number of words after the cursor, you can use dw (multiple words can be deleted using 3dw for 3 words for example). For deleting around the cursor in general, you can use daw.

Comments

2

In old vi, b moves the cursor to the beginning of the word before cursor, w moves the cursor to the beginning of the word after cursor, e moves cursor at the end of the word after cursor and dw deletes from the cursor to the end of the word.

If you type wbdw, you delete the word around cursor, even if the cursor is at the beginning or at the end of the word. Note that whitespaces after a word are considerer to be part of the word to be deleted.

Comments

2

Not exactly sure about your usage of "before" and "after", but have you tried

dw 

?

Edit: I guess you are looking for something to use in insert mode. Strangely enough, there is nothing to be found in the docs, ctrl-w seems to be all there is.

Comments

2

I made two mappings so I could get consistency in insert and out of insert mode.

:map <'Key of your choice'> caw

:imap <'Key of your choice'> <'Esc'> caw

Comments

0

I'd like to delete not only the word before cursor, but the word after or around cursor as well.

In that case the solution proposed by OP (i.e. using <c-w>) can be combined with the accepted answer to give:

inoremap <c-d> <c-o>daw<c-w> 

alternatively (shorter solution):

inoremap <c-d> <c-o>vawobd 

Sample input:

word1 word2 word3 word4 ^------------- Cursor location 

Output:

word1 word4 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.