6

I often use the Ctrl-w command in "Insert" mode to delete the word behind the cursor, just like I would do in Bash.

In Bash, I also have a Meta-d (Esc-d) command for deleting the word in front of the cursor. Does Vim have a similar command for "Insert" mode?

3 Answers 3

7

I've the following mappings for this

noremap  <C-Del>   dw
inoremap <C-Del>   <space><esc>ce

noremap  <C-S-Del> dW
inoremap <C-S-Del> <esc>lcW

As you can see, there are several ways to do it (and handle single letters). and I'm quite certain there has been other Q/A|tips on the topic elsewhere.

NB: unlike <c-o>de that I've seen elsewhere, these mappings handle wor|d word correctly. <c-o>dw would work though, the difference is that the space after the current word will also be deleted while my mappings leave it alone.

2
  • 1
    In my terminal, <Del> and <C-S-Del> seem to generate the same sequences: <Esc>[P. <C-Del> and <S-Del> are distinct (<Esc>[M and <Esc>[2K). Just FYI in case it doesn't work for some: this is probably the reason. Commented May 5, 2020 at 3:21
  • Indeed. vim in terminal has a limited set of possible keybindings -- without knowing how to tweak the terminal used. I can use the exact keybinding I propose because I'm using gvim. Don't hesitate to use one that works and that makes sense to you. Commented May 5, 2020 at 9:42
5

There's a good answer to your question here: https://stackoverflow.com/a/15703188/5031295

In short, ctrl+o or ctrl+Olets you perform one normal mode command while in insert mode. Hence, pressing ctrl+odw while in insert mode deletes one word to the right of cursor (provided the cursor is on the first letter of the word). You can play around with this and see if it suits you. (Using ctrl+[ to get out of Insert Mode helps me a lot; I prefer to use built-in vim commands as much as possible.) Cheers.

6
  • 1
    Vim doesn't distinguish between <c-o>, <C-o>, <c-O> or <C-O>. And as I've noted, <c-o>dw will also erase the space after the current word. This may be what OP wants, I don't know. If not, more convoluted actions are required like <space><esc>ce or <esc>lcw Commented May 9, 2020 at 11:08
  • @LucHermitte your points are true, my bad. I was just offering an out-of-the-box-only answer, as I had the same issue as the OP using cmd line shortcuts.
    – jbeku
    Commented May 9, 2020 at 11:51
  • Thank you - how can I get the help text? If I try :h Ctrl-O, I get help for jumps
    – Shuzheng
    Commented May 10, 2020 at 13:29
  • @LucHermitte, I don't see <c-o> anywhere in the :h? Ctrl-o and Ctrl-O are common notation, but "Control" is normally not represented with lowercase "c"?
    – Shuzheng
    Commented May 10, 2020 at 13:38
  • <c-... is mapping syntax while [mode_]CTRL-... is help syntax. You'll found related entries in the documentation with :h <C- and :h i_CTRL-O. This site has another kdb-something syntax I never remember. Regarding the fact case doesn't matter here with ctrl, I don't know if it's documented. Commented May 10, 2020 at 15:00
1

An alternative to creating a mapping for this single Readline command, is to install the rsi.vim plugin, which emulates all (or at least, many) of Readline's standard shortcuts in insert and command-line modes.

Future readers may also be interested in readline.vim, which purports to provide a more faithful set of mappings, but only in command line mode.

Your Answer

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

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