Mantas
Mantas

Reputation: 5951

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.

Upvotes: 280

Views: 213024

Answers (16)

0xF
0xF

Reputation: 3718

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

Upvotes: 18

builder-7000
builder-7000

Reputation: 7647

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

Upvotes: 0

mycelo
mycelo

Reputation: 429

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.

Upvotes: 6

Naseeruddin V N
Naseeruddin V N

Reputation: 625

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

Upvotes: 13

krmld
krmld

Reputation: 1348

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

daW

To delete just one word:

daw 

Upvotes: 32

Whaledawg
Whaledawg

Reputation: 4286

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

Upvotes: 86

userFog
userFog

Reputation: 11519

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

Upvotes: 2

steve
steve

Reputation: 1259

I think it's just daw

daw - delete a word

Upvotes: 125

R. Aguirre
R. Aguirre

Reputation:

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.

Upvotes: 3

Dan Olson
Dan Olson

Reputation: 23377

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.

Upvotes: 21

Amjith
Amjith

Reputation: 23874

Normal mode:

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

Upvotes: 683

innaM
innaM

Reputation: 47879

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.

Upvotes: 2

Andy
Andy

Reputation: 3854

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.

Upvotes: 85

mouviciel
mouviciel

Reputation: 67879

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.

Upvotes: 2

user36457
user36457

Reputation:

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.

Upvotes: 2

Nadia Alramli
Nadia Alramli

Reputation: 115011

Do you mean like?

dw

Upvotes: 53

Related Questions