Chrome-like Tab behavior in Vim on Mac OS X

March 24, 2015

Google Chrome has trained my brain that Ctrl+T, and ⌘+⌥+[arrows] will move me to the next and previous tabs. Here’s how to make vim work the same way. In .vimrc…

" Ctrl+t to open new tab
" Note: this overwrites a shortcut for the tag stack, if you use tags
nnoremap <C-t> :tabnew

" Chrome-like shortcuts for prevtab and nexttab
" (Meaning ⌘+⌥+[arrows])
nnoremap <ESC>[1;9D gT
nnoremap <ESC>[1;9C gt

YMMV on the exact mappings to use, see below on how to figure it out for yourself. I found a lot of solutions that didn’t work for me, including pressing the key combo in vim under Visual Line mode, so it would show me vim’s notation for what keys it’s getting. But the problem was three things failing in some combination:

– The terminal was mangling the input before it got to vim

– Vim wasn’t showing me the starting “^[” (escape key)

– Vim wasn’t displaying the [1; section of the input either. Just “:9C” or “:9D”

After a lot of searching, I finally found this simple solution to figure out what the terminal is actually sending, and translate to vim speak, all in one. Using the terminal on my Macbook Pro, it’s surprisingly simple:

> sed -n l
[Press whatever key combination you want]

The output it showed me was “^[[1;9D”, so my key signature to map was “[1;9D”

You can see my entire .vimrc file on Github. Hat tip to Adam Morse for the warning on Ctrl+T and the tag stack.