views:

1421

answers:

10

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 found was that ^+W to delete word BEFORE cursor...

A: 

Do you mean like?

dw
Nadia Alramli
+3  A: 

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.

Andy
A: 

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.

John T
A: 

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.

innaM
A: 

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.

mouviciel
+10  A: 

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.

Dan Olson
+1 for reading the question correctly, amongst other things.
Andy
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
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.
Great point blixtor, I'll edit.
Dan Olson
+5  A: 

Normal mode:

daw : delete the word under the cursor    
caw : delete the word under the cursor and put you in insert mode
jinxed_coder
+5  A: 

I'm curious why the one that tells you it can't be done has been upvoted to first place when it can be done pretty easily :/ But whatever...

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
Whaledawg
thanks a lot!(beer)
Mantas
for extra points swap d with c and remove trailing i ;-)
A: 

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.

A: 

I think it's just daw delete a word

steve