5

I frequently use <C-w> in insert mode to back up when I mistype things. I've noticed some odd behavior if I start insert mode when in the middle of a word.

For example, if I have the following line (cursor location indicated by ^):

I love to use CamelCase
                   ^

When I am at that cursor location and do the key combination i<End><C-w>, I am left with the following:

I love to use Camel
                   ^

I expected that <C-w> would erase up to the beginning of CamelCase since that is a word. Is <C-w> only supposed to erase up to the beginning of where I started inserting? Or is there some setting I have to flip to get it to delete the entire word?

If I press <C-w> again, it does erase the rest of the word. I just wasn't expecting a word boundary to appear where insert mode started.

This is using Vim 8.1 (2018 May 18) for Windows.

1
  • The same issue happens for <c-u>.
    – user202729
    Aug 4, 2020 at 3:34

3 Answers 3

5

This is documented behaviour. Deleting text before the insert start location is controlled by the 'backspace' option, and :help 'backspace' says this:

start: allow backspacing over the start of insert; CTRL-W and CTRL-U stop once at the start of insert.


If you really want <C-w> to behave as you "expect", here's a convoluted mapping (the different handling depending on whether the cursor is at the end of the line or not is to make sure that the last character is handled correctly: if you do db when the cursor is at the end of the line, the last character is not deleted; if you do vbd when the cursor is not at the end of the line, the following character is deleted):

inoremap <expr> <C-w> col('.') == len(getline('.'))+1 ? "\<C-o>vb\"_d" : "\<C-o>\"_db"
7

As of Vim patch 8.2.0590 you can use :set backspace+=nostop to make backspace and CtrlW not stop at the insertion point.

1
  • Great! I missed that one.
    – gildux
    Feb 16 at 20:34
1

An easier remap for <c-w> is:

ino <c-w> <c-\><c-o>i<c-w>

Note that this also moves the insert start location, which may changes the behavior of <c-u> or <bs> (depends on 'backspace' option)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.