6

(I put the tl;dr bit at the top)

How can i bind F10 for Vim so that when I hit F10 while inside command (Ex) mode that the command in the command buffer is not actually executed?

I have already tried the one thing that I know to do for the situation which is

cnoremap <F10> <ESC>

Background information follows...

I have a binding which for historical (arbitrary) reasons I have been using the F10 key to conditionally control my unix systems. So basically what I needed to tell Vim to do is when I hit F10 either have this function the same as the Esc key (to exit insert mode for example) or do some other action if already in normal mode (such as move to next pane in tmux).

So that is the background that you didn't ask for about why I am binding Esc rather than mapping my Esc key at the OS level. It's a "smart" unintuitive crazy thing but it's intuitive for yours truly now :)

This has worked fantastically well through a small number of hundreds of thousands of lines of code over many years since I started doing this but one quirk remains.

6

esc in command line mode traditionally meant the same as Enter. That means, it executes your entered command and Vim behaves still the same, when is mapped to another key. Several years ago, I even made a patch, to fix this, but it seems nobody cared too much about it.

Back to your question, simply map F10 to ControlC should do what you want, e.g. abort the entered command:

:cnoremap <f10> <c-c>
  • Oh wow I cannot believe I forgot to test what Esc does! Thanks! I used ctrl+c a lot in the past but it never occurred to me to try this. – Steven Lu Feb 2 '16 at 6:46
  • 1
    As I fix this I find no less than 4 other examples in my vimrc of cnoremap's to <c-c>. This means that I knew about this aspect of Vim in the past. I knew it well. And then I completely forgot about it. Face, meet palm – Steven Lu Feb 2 '16 at 7:00

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.