[vim] delete word after or around cursor in VIM

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.

This question is related to vim keyboard-shortcuts

The answer is


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

I think it's just daw

daw - delete a word

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.


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

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

daW

To delete just one word:

daw 

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.


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.


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.


Normal mode:

daw : delete the word under the cursor    
caw : delete the word under the cursor and put you in insert mode 

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


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.


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.


Do you mean like?

dw

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


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

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.