If you happen to use emacs and vim, your muscle memory might from time to time use the shortcuts from one application in the other. At least that's what happens to me.

Personally, I use evil to give emacs modal editing capabilities. Its user interface is modeled after vi(m). So everything is fine on the emacs front.

I do prefer vi's interface to the default emacs interface, so naturally I don't miss much when using vim. However, there is one short-cut, that I keep hitting while I'm in vim. And that's emacs' "\\" for saving the currently focused buffer's file. And if the binding would just be missing, that wouldn't be so bad. But what's worse is, that vim does not disable the terminal's flow control while it's active. I can't understand why any full screen terminal application would leave it enabled (and sadly, vim is not alone in choosing to behave this way...). Because of this, the terminal vim is running in freezes as soon as you hit "\" (you can obviously un-freeze it, by hitting "\", but ... come on). It's annoying.

So. The shell to rescue: Let's create a wrapper function ‘vim’, that takes care of disabling flow control for us (handling flow control is a non-issue in gvim):

vim () {
    # ‘local’ is not a POSIX sh feature, but if you don't use something
    # like zsh, mksh, bash or ksh93 as your interactive shell, I can't
    # help you anyway. ;)
    local terminal="$(stty -g)"
    command stty -ixon
    command vim "$@"
    command stty "$terminal"

Now that that's out of the way, to get the emacs short-cut working in both insert and normal mode, use this:

inoremap <c-x><c-s> <c-o>:w<cr>
noremap <c-x><c-s> :w<cr>

And that's it.


This obviously only works if you start vim from your shell. If an application starts the editor, it doesn't care much about the shell function that you defined. To cover these cases as well, you can create a wrapper script, that does what the function does. But the real question is, why vim doesn't disable flow control itself - if not by default, then why is it not an option. (And no, I do not want to disable flow control altogether, because it might come in handy with non-full-screen terminal applications, that generate a lot of output.)

Posted Thu 19 Dec 2013 13:49:31 CET Tags: