0

I'm re-learning Vim. My question is that I want to option + to backward-delete a word in INSERT MODE. I've spent some days to achieve this (and succeeded) to my zsh shell through bindkey in .zshrc file (not related to Vim though), but now in Vim this problem occurs again. So my question is how to bind this action backward-kill-word to the key combination?

1
2

One option is instead of trying to bind a key combination to this action, just use the default key binding for it, which is Ctrl+W.

See :help i_CTRL-W.

This same key binding should be available by default in zsh (or bash, etc.) so you could just use it everywhere with no need for custom configuration.

You could try to map Option+ to do the same in Vim... But it's hard to tell whether that will work for you right away, since mappings with the Option key can be tricky to get right... In theory, this should be the way to map it:

noremap! <M-BS> <C-w>

But whether that's going to work depends on your terminal and Vim configuration and possibly on which kind of terminal emulator you're using...

UPDATE: From your answer, it seems Option+ actually produces Shift+Insert on your terminal (perhaps through some configuration of yours.)

So you might want to use this mapping:

noremap! <S-Insert> <C-w>

Using map! instead of imap will make the Option+ also work in command-line mode (when you start a command with :, or also when you start search with /.)

You should use nore (non-recursive) mappings in most cases. See “Strict Mapping” from Steve Losh’s “Learn Vimscript the Hard Way” for a great explanation of why you usually want that. Vim documentation also mentions it under :help recursive_mapping.

5
  • 1
    lol, I'm writing an answer while I see your answer, from the comment I actually solved this problem. Just a short question: I don't understand noremap's meaning, does it mean no-remap? So I don't want the result to be remapped again? Still great thanks for your kindness! – job_start Jan 3 at 19:41
  • 1
    Glad you found it! It's actually nore-map and the nore means non-recursive. (See :help recursive_mapping.) It means it will always use the original Vim meaning of <C-w> (which is backward-kill-word), even if you end up remapping <C-w> to something else in a separate mapping. You usually want to use nore mappings, with few exceptions. That way, even if you remap something used in the expansion, the mapping won't be affected by the new mappings. – filbranden Jan 3 at 20:11
  • 1
    Actually, here's the best explanation for that: learnvimscriptthehardway.stevelosh.com/chapters/05.html – filbranden Jan 3 at 20:12
  • Updated my answer to include all that. – filbranden Jan 3 at 20:18
  • 1
    I think I have read the best one from the second comment, very clear to me! – job_start Jan 3 at 21:25
1

(First I think I have to say is that my environment is simply Terminal and zsh, all I use are built-in stuffs provided by macOS.)

In INSERT MODE I typed option + then strangely

<S-Insert>

appeared (I think this is because I modified the Terminal preferences to make option + be mapped to \033[2;2~) , then I used the following and it works:

imap <S-Insert> <C-W>

(But actually I don't know what's the difference between imap and inoremap as the docs is a bit long and I decided to try-then-confirm rather than reading.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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