Shared publicly  - 
 
Mintty: Avoiding escape timeout issues in vim and TMUX

What an awesome combo! Cygwin Mintty with TMux (native cygwin OR SSH). I switched to ViM from Eclipse/Sublime a few months ago and initially regretted switching to this primitive text editor--weeks of lost productivity and no end in sight.

Anyway, about a month ago I became more comfortable with ViM and while I'm far from a ViM expert, I feel like the TMuX / ViM combo is starting to show its true colors. I'm really ecstatic that I stuck with this combo and didn't give up. 

Anyway, enough rambling--ViM with Tmux has its own caveats but the one that was bothering me more and more was the idle 500msec escape timeout after switching from Insert to Normal mode. Vim can be configured to emit a special keysequence which Mintty registers as a special "app escape mode"--in this mode hitting the ESC key would send a unique sequence "\eO[" instead of the regular "\[" sequence. Using this, we could configure ViM to escape immediately instead of waiting for the timeoutlen escape timeout.

Unfortunately, I had accustomed to using "Ctrl-[" (Ctrl + Left Bracket) for escaping to Normal mode instead of hitting ESC key (too far away); Mintty wasn't configured to send the special app escape sequence on Ctrl-[.  I hacked Mintty to send the special app escape sequence on Ctrl-[ with only three lines of code (but I hope I didn't break anything else). Configuring ViM to send the proper escape sequences took some head scratching, but that's working too now.

It's very important that you disable ViM 'timeout' option (set notimeout) with the App Escape mode otherwise intermittent '[' characters appear when escaping. 

vim
set notimeout

set mouse=a
if &term =~ '^screen'
        set ttymouse=xterm2 " tmux knows the extended mouse mode
endif

" Mode dependent cursors for vim

let &t_ti.="\e[1 q"
let &t_te.="\e[0 q"

let &t_SI="\e[5 q"
let &t_EI="\e[1 q"

" mintty app escape key (tmux and non-tmux)
" https://code.google.com/p/mintty/wiki/Tips#Avoiding_escape_timeout_issues_in_vim
if &term =~ '^screen'
  let &t_ti="\ePtmux;\e\e[?7727h\e\\".&t_ti
  let &t_te="\ePtmux;\e\e[?7727l\e\\".&t_te
else
  let &t_ti="\e[?7727h".&t_ti
  let &t_te="\e[?7727l".&t_te
endif
noremap <Esc>O[ <Esc>
noremap! <Esc>O[ <Esc>

mintty patch
--- wininput.c  (revision 1321)
+++ wininput.c  (working copy)
@@-599,6+599,12 @@
   bool ctrl_key(void) {
     bool try_key(void) {
       wchar wc = undead_keycode();
+
+      if (wc == '[' && term.app_escape_key) {
+        ss3('[');
+        return true;
+      }
+
       char c;
       switch (wc) {
         when '@' or '[' ... '_' or 'a' ... 'z': c = CTRL(wc);

More Info:
Andy's instructions for enabling app escape mode: https://code.google.com/p/mintty/wiki/Tips#Avoiding_escape_timeout_issues_in_vim
My patch/bug report to support App Escape mode for Ctrl-[: https://code.google.com/p/mintty/issues/detail?id=405
My vim mintty config fragment: https://github.com/saamalik/dotvim/blob/master/init/mintty.vim
Latest mintty binary with my patch: https://github.com/saamalik/dotvim/tree/master/windows/windows_binaries
1
Add a comment...