Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to swtich tmux pane with alt+arrow in vscode

I am a tmux user and personally perfer switch pane with alt+arrow. However, in vscode, it does not work. Even I tried removing the default keybinding in prevent of overlapping.

I tried binding to alt+u/h/j/k and it work fine. I think there is a problem with alt+arrow key binding in vscode. Is there any setting I did not find or is it a bug?

keybinding.json - vscode

 {
        "key": "alt+up",
        "command": "-workbench.action.terminal.focusPreviousPane",
        "when": "terminalFocus"
    },
    {
        "key": "alt+down",
        "command": "-workbench.action.terminal.focusNextPane",
        "when": "terminalFocus"
    },
    {
        "key": "alt+left",
        "command": "-workbench.action.terminal.focusPreviousPane",
        "when": "terminalFocus"
    },
    {
        "key": "alt+right",
        "command": "-workbench.action.terminal.focusNextPane",
        "when": "terminalFocus"
    }

.tmux.conf

# switch panes with "(alt) + (↑ ↓ ← →)"
## This does not work in vscode integrated terminal
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R

## This do work
bind -n M-u select-pane -U
bind -n M-j select-pane -D
bind -n M-h select-pane -L
bind -n M-k select-pane -R
like image 671
timtor chen Avatar asked Sep 03 '25 06:09

timtor chen


2 Answers

[
  {
    "key": "alt+up",
    "command": "workbench.action.terminal.sendSequence",
    "args": { "text": "\u001b[1;3A" },
    "when": "terminalFocus"
  },
  {
    "key": "alt+down",
    "command": "workbench.action.terminal.sendSequence",
    "args": { "text": "\u001b[1;3B" },
    "when": "terminalFocus"
  },
  {
    "key": "alt+left",
    "command": "workbench.action.terminal.sendSequence",
    "args": { "text": "\u001b[1;3D" },
    "when": "terminalFocus"
  },
  {
    "key": "alt+right",
    "command": "workbench.action.terminal.sendSequence",
    "args": { "text": "\u001b[1;3C" },
    "when": "terminalFocus"
  }
]

In addition to Daniel's answer above, the default behavior of xterm.js when pressing alt + left is to jump backward a word, just like ctrl + left(same for right). In order to let alt + left send the right sequence tmux needs, you have to add the above two keybindings to keybindings.json

like image 156
TPOB Avatar answered Sep 04 '25 21:09

TPOB


The when clause changed so those keybindings aren't actually being removed. You can check this by changing your log level to trace and viewing the devtools console.

A better way to ignore those keybindings is to remove them from being prioritized over sending sequences to the shell via this setting:

  "terminal.integrated.commandsToSkipShell": [
    "-workbench.action.terminal.focusPreviousPane",
    "-workbench.action.terminal.focusNextPane",
    "-workbench.action.terminal.focusPreviousPane",
    "-workbench.action.terminal.focusNextPane",
  ],
like image 33
Daniel Imms Avatar answered Sep 04 '25 21:09

Daniel Imms