I have made a few mappings for my personal configuration file, and I think I am getting a hang of it, however, I am wondering if there are any particular tricks to create mappings for command-line mode.

I know that there are keys like Ctrl+h for deleting the character before the cursor or Ctrl+Left for moving one WORD left, and I know how to remap these, but I am unsure what my options are when I want to make new ones. If I wanted a key (say Ctrl+_) which deletes the content of a parenthesis in insert mode, I would do this with:

imap <C-_> <C-O>dib

However, this relies on using normal mode, and I am unsure how, if possible, I could achieve something like that for command-line mode.

  • inoremap would be more appropriate for your mapping. See :help recursive-mapping.
    – Rich
    Commented Nov 8, 2017 at 14:10

1 Answer 1


Vim provides a few mechanisms for inspecting the contents of the command-line (See Further Reading, below), but not many for directly manipulating it.

One way you could create the mapping you describe above would be to go via the command-line window:

:cnoremap <expr> <c-_> &cedit.'dib'.'<c-c>'

This creates an expression mapping that opens the command-line window, performs a normal mode dib and then returns to the regular command-line. The downside to this method is that it leaves the command-line window open after the mapping completes (although it no longer has focus). The window closes when you enter or cancel the command. See Mass's comment, below for a clever fix.

Your only other real option for complex cmaps is to use Ctrl-\e which allows you to replace the entire command-line with the result of an expression.

Further Reading

  • :help cmdwin
  • :help 'cedit'
  • :help c_CTRL-\_e
  • :help c_CTRL-R_=
  • :help getcmdline()
  • :help getcmdtype()
  • :help getcmdpos()
  • :help setcmdpos()
  • 2
    After <c-c> the command line window is only kind-of open; a redraw will hide it. You could force a redraw like so cnoremap <expr> <c-_> &cedit.'dib'.'<c-c><c-r>=execute("redraw")<cr>'
    – Mass
    Commented Nov 8, 2017 at 16:51
  • @Mass Yeah, I didn't think the distinction was important enough to include in the answer. It didn't occur to me you could use the expression register to execute a :redraw, though. Clever!
    – Rich
    Commented Nov 8, 2017 at 17:04
  • Related Vimcasts episode: Refining search patterns with the command-line window Commented Nov 8, 2017 at 19:05
  • @Rich. Thanks for the answer and the pointers to where I can learn more about it. I will try to make some new mappings to get used to the command line window.
    – Kristian
    Commented Nov 13, 2017 at 21:10

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.