9

I input :map! <C-q> :q <CR> in command line mode, then return to normal mode, and press ctrl-q, but vim does not quit. Why?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
river
  • 694
  • 6
  • 22
  • 1
    [Jan Hudec describes that `^S` can't be mapped](http://stackoverflow.com/questions/7865123/vim-mapping-key-does-not-work/7865307#7865307) -- perhaps `^Q` fails for the same reason? At least for me, `^S` and `^Q` still function for XON and XOFF. – sarnold Oct 25 '11 at 01:53
  • See also http://stackoverflow.com/questions/791765/unable-to-forward-search-bash-history-similarly-as-with-ctrl-r – Josh Lee Oct 25 '11 at 02:28
  • Maybe the answer on http://stackoverflow.com/questions/7652590/is-it-possible-to-map-c-to-in-vim/7653633#7653633 can be applied to your problem. – mMontu Oct 25 '11 at 11:04

2 Answers2

16

As Johnsyweb suggested, Ctrl-Q does not reach Vim in the first place. This holds for the popular Ctrl-S as well. Both can be fixed by adding

silent !stty -ixon > /dev/null 2>/dev/null

to your .vimrc as it forces these control sequences to reach the application.

bitmask
  • 32,434
  • 14
  • 99
  • 159
  • I tested it. It works on my machine. What shell and what terminal emulator (if any) are you using? – bitmask Oct 25 '11 at 07:02
  • @river - try putting `stty -ixon` in your .bashrc instead. Launch a new terminal, then vim. – lxs Mar 03 '14 at 11:23
  • 1
    @Ixs tested stty -ixon with putty connected to a putty-256color terminal and worked fine - thanks – dcompiled Jan 16 '16 at 16:59
10

Why anyone would want to add yet another way of quitting Vim to the plethora already available is beyond me. However...

On Fedora 15, Ctrl-Q is likely being captured by your terminal as XON (resume) and therefore not reaching Vim to quit the application.

You can check if there's an error in your mapping or a clash like so:

:verbose map! <C-Q>

:map! is for specifing mappings in insert and command-line modes.

normal-mode mappings are (usually) specified like so:

:nmap <C-Q> :q<CR>
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • I am using Fedora 15. But :map! :q also not work, and I have tried many other key combination. – river Oct 25 '11 at 04:38
  • @Downvoter: can you please point out what's wrong or missing from this answer? – johnsyweb Mar 03 '14 at 20:14
  • `:nmap` will work in vanilla `vim`, too. The control character isn't getting to the application in terminal mode for the reason I outlined. [bitmask's answer](http://stackoverflow.com/a/7884226/78845) details how you can solve this. – johnsyweb May 21 '14 at 07:30
  • Ctrl+q can be done one-handed (2 keys pressed). :q enter requires two hands (4 keys pressed( shift+; q enter )). One might conclude that ctrl+q takes around half the time as :q enter, this makes it more efficient. – zeros-and-ones Dec 19 '14 at 19:08
  • @zeros-ones: `ZZ` is my preferred exit strategy. – johnsyweb Dec 08 '15 at 21:49
  • @Johnysweb Is that a custom solution? Seems like I have heard of that before. – zeros-and-ones Dec 12 '15 at 01:06
  • @zeros-ones: It's standard and documented here: http://vimdoc.sourceforge.net/htmldoc/editing.html#ZZ – johnsyweb Dec 12 '15 at 03:41
  • 1
    Thanks for the link @Johnysweb – zeros-and-ones Dec 18 '15 at 22:14