Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96478

Forward / backward words in the VSCode terminal

What commandID's govern moving forward or backward a word in the VSCode terminal? (say running Zsh)

Background

I know that these commands are bound Option + Right and Option + Left respectively since I can try that in the terminal and the cursort moves forward and backward one word accordingly.

When I search for those keybindings in the Keyboard Shortcuts menu, I find the following commandIDs tied to them:

However I'm observing here something odd. If I bind both commands to Command+F and Command + B respectively (with no when clause), I'm not able to use these keybindings to move forward/backward a word in the terminal

Upvotes: 2

Views: 5939

Answers (2)

Andrea Maruccia
Andrea Maruccia

Reputation: 31

I'm on macos and I needed to do this:

    {
        "key": "cmd+r",
        "command": "workbench.action.terminal.sendSequence",
        "args": { "text": "\u0012" }, // reverse command search
        "when": "terminalFocus"
      },
      {
        "key": "cmd+c",
        "command": "workbench.action.terminal.sendSequence",
        "args": {
          "text": "\u0003" // send interrupt, cancel command
        },
        "when": "terminalFocus"
      },
      {
        "key": "cmd+left",
        "command": "workbench.action.terminal.sendSequence",
        "args": {
            "text": "\u001b[1;5D" // cursor one word left
        },
        "when": "terminalFocus"
      },
      {
        "key": "cmd+right",
        "command": "workbench.action.terminal.sendSequence",
        "args": {
            "text": "\u001b[1;5C" // cursor one word right
        },
        "when": "terminalFocus"
      },

hope it helps somebody

Upvotes: 3

Mark
Mark

Reputation: 183104

I'm no expert in this area but I assume that the + / keybinding is being handled by your shell – vscode has nothing to do with that. So you won’t find a command that vscode is using to skip forward or backward in the terminal.

But you can implement one yourself by sending the control codes to the shell. For that, modify your keybinding settings (keybindings.json). You can open it with the command palette (accessible with + + P), searching for “Open Keyboard Shortcuts (JSON)”. Then make an addition like:

{
  "key": "ctrl+b",          // whatever keybinding you wish
  "command": "workbench.action.terminal.sendSequence",
  "args": {
      "text": "\u001b[;5D"
  },
  "when": "terminalFocus && !terminalTextSelected"
},
{
  "key": "ctrl+f",
  "command": "workbench.action.terminal.sendSequence",
  "args": {
      "text": "\u001b[;5C"
  },
  "when": "terminalFocus && !terminalTextSelected"
},

Swap cmd for ctrl depending on your OS. You didn’t specify what shell you’re using but it works on Git Bash and CMD.exe on Windows – all I can test on here.

\u001b[ is the control sequence introducer, so that what follows is interpreted as a control sequence and not as literal characters.

The 5D is the same as Ctrl + . The 5 is Ctrl. The D is cursor back. You can find further bindings in the section “PC-Style Function Keys” of the ctlseqs documentation. I believe the ; is necessary to indicate that the following is a control character, in this case 5 for Ctrl, and not the digit 5.

Upvotes: 6

Related Questions