Search code examples
vimtmuxkey-bindings

vim key-biding failing when inside of tmux


I use vim as my IDE and tabs inside of it. Instead of changing tabs with gt, gT I mapped Ctrl PageUp and Ctrl PageDown for that, so my .vimrc looks like this:

nnoremap <C-PageUp> gT
nnoremap <C-PageDown> gt

It works just fine outside of tmux... but then, when I open vim from within tmux, and try to change tabs with Ctrl PageUp or Ctrl PageDown it doesn't and it encloses the current word under the cursor in brackets and puts part of it in uppercase... but it won't change the tab in vim. I checked in tmux key-binding if there was anything bound to C-PageUp or C-PageDown but there was nothing. Still I tried to unbind it in .tmux.conf like this, but it won't work:

unbind-key C-PageUp
unbind-key C-PageDown

Changing tabs in vim with gt and gT works just fine even inside of tmux, but I'm so used to Ctrl PageUp/Down that I would like to keep that.

About the strange behavior...

When in vim within tmux, if my cursor is on the letter t of the word function, and I press Ctrl PageUp or Ctrl PageDown, the word function is turned into [funcTION]. I do have in my .vimrc mappings the following:

inoremap [<cr> [<cr>]<c-o><s-o>
inoremap [ []<left>
inoremap <expr> ] strpart(getline('.'), col('.')-1, 1) == "]" ? "\<Right>" : "]"
nnoremap [ mmbi[<esc>ea]<esc>`m<right>
vnoremap [ <Esc>`<i[<Esc>`>a<right>]<Esc>

They all handle the insertion of closing squared brackets (if you open a square bracket, a closing one is put automatically on the screen). I do have the same for parenthesis, curly brackets, quotes, etc. That 4th one allows me to go in vim normal mode press [ and it will enclose the word in brackets, that is why I thought this could be influencing the changing of tabs somehow, but I commented all of those lines and it didn't help.

So, my question is, how I identify what is hijacking my key-binding and maybe sending something else to vim? As it only happens when inside tmux, I thought tmux config would be a strong candidate as the hijacker.

What I'm using: VIM 8.1 tmux 3.0a

EDIT: Description of the Ctrl PageDown in terminal, with and without tmux, in vim with and without the .vimrc loaded enter image description here


Solution

  • I finally found it. Apparently the default terminal in tmux (screen) does not support extended keys like Ctrl PageDown, so you have to change your default terminal. For me it worked with xterm-256color. For that you add this to .tmux.conf set -g default-terminal xterm-256color. The discussion about it is here