How to delete a WORD in vim in insert mode

663 Views Asked by At

Is there a way how to delete a whole WORD (e.g. from vim path/to/some/file.txt to vim) in insert mode?

To delete a word in insert mode the C-W shortcut works well. I got used to use C-W in bash too but bash actually deletes whole WORDs.

Now I'd love to be able to delete whole WORDs in insert mode. How can I achieve this in vim?

2

There are 2 best solutions below

0
On BEST ANSWER

There's only :help i_CTRL-W for words, and :help i_CTRL-U for the entire line, but nothing for WORDs. You can easily define a custom mapping, though:

:inoremap <C-B> <Esc>"_dB"_s

Here's a more elaborate version, with better handling of corner cases:

inoremap <silent> <expr> <C-B>
\   col('.') == 1 ?
\       '<C-w>' :
\       col('.') >= col('$') ?
\           '<C-o>:normal! B"_d$<CR>' :
\           '<C-o>:normal! m"B"_dg`"<CR>'
1
On

Here is my approach:

:inoremap <silent> <C-w> <C-\><C-O>d<C-Left>