Using Alt+Arrow keys in vim inside tmux

tmux seems to send different key sequences to vim, so I had to add both to vimrc:

nmap :tabprev
" Alt+left arrow inside tmux
nmap ^[[1;3D :tabprev
" Alt+left arrow outside tmux
nmap ^[[1;9D :tabprev
nmap :tabnext
" Alt+right arrow inside tmux
nmap ^[[1;3C :tabnext
" Alt+right arrow outside tmux
nmap ^[[1;9C :tabnext

 

You’ll need to use Ctrl+v to input the keys to vimrc, see this article.

And iTerm2 needs to be configured, Left Alt Key (or right if you want) = Esc+

Relevant articles (not sure what their relations are):