Binding arrow keys for TMUX with MacOS Terminal

Viewed 1870

I am trying to create new arrow key bindings in TMUX which is running on a Linux machine. I ssh to that machine from my Mac (Mojave 10.14.6) using the Terminal. These are all combinations that I added in ~/.tmux.conf:

# Use Shift-Arrow to move between panes
bind -T root C-Left select-pane -L
bind -T root C-Right select-pane -R
bind -T root C-Up select-pane -U
bind -T root C-Down select-pane -D

# Use Alt-Shift Left/Right to switch between tabs
bind -T root M-C-Left previous-window
bind -T root M-C-Right next-window

# Use Alt-Shift Up/Down to create/close a tab
bind -T root M-C-Up new-window
bind -T root M-C-Down kill-pane

It works for Ctrl+Left and Ctrl+Right, but not for the others. For the Up/Down arrow combinations it always goes through the Terminal history, so I think that Terminal is overriding these bindings. However, I can not find where it does so, I removed all relevant combinations from Preferences -> Profiles -> Keyboard and also from System Preferences -> Keyboard -> Shortcuts. For the M-C-Left/Right nothing happens.

I believe the TERM variable is set correctly:

TERM=xterm-256color
TERM_PROGRAM=Apple_Terminal
TERM_PROGRAM_VERSION=421.2
TERM_SESSION_ID=782B88DE-A8DA-4542-8428-F9D5C1EF6386

The key bindings are also listed when calling tmux list-keys. When pressing the combinations in cat, I get:

cat
  ^[[A  (Ctrl+Up)
  ^[[B  (Ctrl+Down)
  ^[^[[C  (Ctrl+Option+Right)
  ^[^[[D  (Ctrl+Option+Left)
  ^[^[[A  (Ctrl+Option+Up)
  ^[^[[B  (Ctrl+Down)

In the TMUX server log, key bindings seem to be loaded properly. An excerpt when starting TMUX and then trying the key bindings:

1629805569.985046 server started, pid 16654
1629805569.985178 continuing cmdq 0x55797a7cedb0: flags=0, client=-1
1629805569.985192 cmdq 0x55797a7cedb0: bind-key C-b send-prefix
1629805569.985217 continuing cmdq 0x55797a7cedb0: flags=0, client=-1
...
1629805569.986939 socket path /tmp/tmux-1038/default
1629805569.987071 new client 7
1629805569.987113 loading /home/zimmerl/.tmux.conf
1629805569.987142 /home/zimmerl/.tmux.conf: # Move status bar to the top
1629805569.987148 /home/zimmerl/.tmux.conf: set-option -g status-position top
1629805569.987159 /home/zimmerl/.tmux.conf: 
1629805569.987165 /home/zimmerl/.tmux.conf: # Change colors
1629805569.987178 /home/zimmerl/.tmux.conf: set -g status-fg green
1629805569.987190 /home/zimmerl/.tmux.conf: set -g status-bg black
1629805569.987201 /home/zimmerl/.tmux.conf: 
1629805569.987208 /home/zimmerl/.tmux.conf: # Use Shift-Arrow to move between panes
1629805569.987215 /home/zimmerl/.tmux.conf: bind -T root C-Left select-pane -L
1629805569.987228 /home/zimmerl/.tmux.conf: bind -T root C-Right select-pane -R
1629805569.987239 /home/zimmerl/.tmux.conf: bind -T root C-Up select-pane -U
1629805569.987251 /home/zimmerl/.tmux.conf: bind -T root C-Down select-pane -D
1629805569.987262 /home/zimmerl/.tmux.conf: 
1629805569.987269 /home/zimmerl/.tmux.conf: # Use Alt-Shift Left/Right to switch between tabs
1629805569.987276 /home/zimmerl/.tmux.conf: bind -T root M-C-Left previous-window
1629805569.987288 /home/zimmerl/.tmux.conf: bind -T root M-C-Right next-window
1629805569.987299 /home/zimmerl/.tmux.conf: 
1629805569.987306 /home/zimmerl/.tmux.conf: # Use Alt-Shift Up/Down to create/close a tab
1629805569.987313 /home/zimmerl/.tmux.conf: bind -T root M-C-Up new-window
1629805569.987323 /home/zimmerl/.tmux.conf: bind -T root M-C-Down kill-pane
1629805569.987330 /home/zimmerl/.tmux.conf: 
1629805569.987335 /home/zimmerl/.tmux.conf: # Use Ctrl+b -> h/v to split current pane horizontally/vertically
1629805569.987340 /home/zimmerl/.tmux.conf: bind -T prefix h split-window -h -c "#{pane_current_path}"
1629805569.987354 /home/zimmerl/.tmux.conf: bind -T prefix v split-window -v -c "#{pane_current_path}"
1629805569.987369 continuing cmdq 0x55797a7d4c80: flags=0, client=-1
1629805569.987375 cmdq 0x55797a7d4c80: set-option -g status-position top
1629805569.987390 cmdq 0x55797a7d4c80: set-option -g status-fg green
1629805569.987399 cmdq 0x55797a7d4c80: set-option -g status-bg black
1629805569.987408 cmdq 0x55797a7d4c80: bind-key -T root C-Left select-pane -L
1629805569.987421 cmdq 0x55797a7d4c80: bind-key -T root C-Right select-pane -R
1629805569.987434 cmdq 0x55797a7d4c80: bind-key -T root C-Up select-pane -U
1629805569.987446 cmdq 0x55797a7d4c80: bind-key -T root C-Down select-pane -D
1629805569.987457 cmdq 0x55797a7d4c80: bind-key -T root M-C-Left previous-window
1629805569.987469 cmdq 0x55797a7d4c80: bind-key -T root M-C-Right next-window
1629805569.987480 cmdq 0x55797a7d4c80: bind-key -T root M-C-Up new-window
1629805569.987490 cmdq 0x55797a7d4c80: bind-key -T root M-C-Down kill-pane
1629805569.987504 cmdq 0x55797a7d4c80: bind-key -T prefix h split-window -h -c #{pane_current_path}
1629805569.987518 cmdq 0x55797a7d4c80: bind-key -T prefix v split-window -v -c #{pane_current_path}
1629805569.987529 unref client 7 (2 references)
1629805569.987553 event dispatch enter
1629805569.987591 got 100 from client 7
...
1629805569.987882 continuing cmdq 0x55797a7d3940: flags=0, client=7
1629805569.987891 cmdq 0x55797a7d3940: new-session
1629805569.987901 new term: xterm-256color
1629805569.988166 xterm-256color override: XT 
1629805569.988178 xterm-256color override: Ms \033]52;%p1%s;%p2%s\a
1629805569.988187 xterm-256color override: Cs \033]12;%p1%s\a
1629805569.988194 xterm-256color override: Cr \033]112\a
1629805569.988202 xterm-256color override: Ss \033[%p1%d q
1629805569.988209 xterm-256color override: Se \033[2 q
1629805569.988248 new key \033Oo: 0x103c (KP/)
1629805569.988255 new key \033Oj: 0x103d (KP*)
1629805569.988261 new key \033Om: 0x103e (KP-)
1629805569.988267 new key \033Ow: 0x103f (KP7)
1629805569.988273 new key \033Ox: 0x1040 (KP8)
1629805569.988280 new key \033Oy: 0x1041 (KP9)
1629805569.988286 new key \033Ok: 0x1042 (KP+)
1629805569.988292 new key \033Ot: 0x1043 (KP4)
1629805569.988297 new key \033Ou: 0x1044 (KP5)
1629805569.988302 new key \033Ov: 0x1045 (KP6)
1629805569.988307 new key \033Oq: 0x1046 (KP1)
1629805569.988313 new key \033Or: 0x1047 (KP2)
1629805569.988319 new key \033Os: 0x1048 (KP3)
1629805569.988325 new key \033OM: 0x1049 (KPEnter)
1629805569.988330 new key \033Op: 0x104a (KP0)
1629805569.988337 new key \033On: 0x104b (KP.)
1629805569.988342 new key \033OA: 0x1038 (Up)
1629805569.988347 new key \033OB: 0x1039 (Down)
1629805569.988354 new key \033OC: 0x103b (Right)
1629805569.988359 new key \033OD: 0x103a (Left)
1629805569.988365 new key \033[A: 0x1038 (Up)
1629805569.988370 new key \033[B: 0x1039 (Down)
1629805569.988377 new key \033[C: 0x103b (Right)
1629805569.988384 new key \033[D: 0x103a (Left)
1629805569.988390 new key \033OH: 0x1033 (Home)
1629805569.988396 new key \033OF: 0x1034 (End)
1629805569.988402 new key \033[H: 0x1033 (Home)
1629805569.988408 new key \033[F: 0x1034 (End)
1629805569.988413 new key \033Oa: 0x5038 (C-Up)
1629805569.988420 new key \033Ob: 0x5039 (C-Down)
1629805569.988427 new key \033Oc: 0x503b (C-Right)
1629805569.988432 new key \033Od: 0x503a (C-Left)
1629805569.988438 new key \033[a: 0x9038 (S-Up)
1629805569.988444 new key \033[b: 0x9039 (S-Down)
1629805569.988450 new key \033[c: 0x903b (S-Right)
1629805569.988457 new key \033[d: 0x903a (S-Left)
...
1629805569.989201 replacing key \033OH: 0x1033 (Home)
1629805569.989206 replacing key \033OF: 0x1034 (End)
...
1629805569.989229 replacing key \033OA: 0x1038 (Up)
1629805569.989236 replacing key \033OB: 0x1039 (Down)
1629805569.989242 replacing key \033OD: 0x103a (Left)
1629805569.989247 replacing key \033OC: 0x103b (Right)
...
1629805569.989292 new key \033[1;2B: 0x9039 (S-Down)
1629805569.989297 new key \033[1;3B: 0x3039 (M-Down)
1629805569.989303 new key \033[1;4B: 0xb039 (M-S-Down)
1629805569.989308 new key \033[1;5B: 0x5039 (C-Down)
1629805569.989314 new key \033[1;6B: 0xd039 (C-S-Down)
1629805569.989319 new key \033[1;7B: 0x7039 (C-M-Down)
...
1629805569.989546 new key \033[1;2C: 0x903b (S-Right)
1629805569.989551 new key \033[1;3C: 0x303b (M-Right)
1629805569.989557 new key \033[1;4C: 0xb03b (M-S-Right)
1629805569.989567 new key \033[1;5C: 0x503b (C-Right)
1629805569.989574 new key \033[1;6C: 0xd03b (C-S-Right)
1629805569.989580 new key \033[1;7C: 0x703b (C-M-Right)
1629805569.989943 new key \033[1;2A: 0x9038 (S-Up)
1629805569.990239 new key \033[1;3A: 0x3038 (M-Up)
1629805569.990245 new key \033[1;4A: 0xb038 (M-S-Up)
1629805569.990830 new key \033[1;5A: 0x5038 (C-Up)
1629805569.991122 new key \033[1;6A: 0xd038 (C-S-Up)
1629805569.991440 new key \033[1;7A: 0x7038 (C-M-Up)
1629805569.991544 @0 alerts flags added 0x2
1629805569.991559 spawn: /bin/bash -- 
...
1629805580.564176 event dispatch enter
1629805580.970822 keys are 3 (\033[A)
1629805580.970848 complete key \033[A 0x1038
1629805580.970859 session 0 not pasting
1629805580.970867 writing key 0x1038 (Up)
1629805580.970873 found key 0x1038: "\033[A"
1629805580.970884 event dispatch exit
....
1629805581.064643 event dispatch enter
1629805582.965826 keys are 3 (\033[B)
1629805582.965857 complete key \033[B 0x1039
1629805582.965870 session 0 not pasting
1629805582.965879 writing key 0x1039 (Down)
1629805582.965887 found key 0x1039: "\033[B"
1629805582.965900 event dispatch exit
3 Answers

Just try to reload the config. Kill definitely your tmux (tmux kill-server) and launch it with:

tmux source-file ~/.tmux.conf

or simply load the config in tmux command mode (Ctrl+B):

:source-file ~/.tmux.conf

I think you should kill existing sessions after changing the configuration of tmux.

tmux kill-server

Now you can create new sessions which will have Changed configurations.

The default terminal schemes map the left and right arrow keys to control codes, which is why they work.

The up and down arrow keys aren't mapped to control codes, which seems to be why tmux isn't picking them up.

I got this to work by modifying my scheme (Preferences > Profiles > Keyboard) to send the control codes for all the arrow keys.

Terminal Keyboard Configuration

Related