DEV Community

Bradford Fults
Bradford Fults Subscriber

Posted on • Edited on

Keybindings in Terminal in VS Code

I’ve been trying to use VS Code’s integrated Terminal function (Ctrl+`) and one of the most annoying things was that some of my long-used keybindings in my shell (used to be bash, now zsh) were not working.

Specifically:

  • Ctrl+r – Do a reverse command search in the terminal.
  • Ctrl+a – Move the cursor to the beginning of the current line.
  • Ctrl+e – Move the cursor to the end of the current line.

I know the latter two originate in Emacs, but I don’t much care about the history as much as I care about matching my 25+ years of muscle memory: I expect the cursor to move and instead I get an ugly ^A or ^E showing up on my terminal line, without any cursor movement.

Anyway, this was a complicated endeavor to figure out, but I resolved it thusly:

Fixing Ctrl+r

I updated the VS Code keybinding for Ctrl+r to just allow VS Code to take over the reverse search dialog entirely, which is fine for me:
# Command: Open Keyboard Shortcuts (JSON)
{ "key": "ctrl+r", "command": "workbench.action.terminal.runRecentCommand", "when": "terminalFocus"}
# Just kidding: this was slow; see below
Enter fullscreen mode Exit fullscreen mode

Update: I reverted to zsh’s Ctrl+r functionality

Because VSCode’s was horribly slow after a few weeks of commands:

# Command: Open Keyboard Shortcuts (JSON)
{
    "key": "ctrl+r",
    "command": "-workbench.action.openRecent"
}
Enter fullscreen mode Exit fullscreen mode

Fixing Ctrl+a and Ctrl+e

First, I disabled keyboard “chords” in the terminal context because I don’t use chords at all, much less in the Terminal:

# Command: Open User Settings (JSON)
"terminal.integrated.allowChords": false
Enter fullscreen mode Exit fullscreen mode

Then I finally figured out that VS Code is booting my zsh shell with different options from Terminal.app, and thus I needed to explicitly set a mode in my .zshrc:

# ~/.zshrc
set -o emacs
Enter fullscreen mode Exit fullscreen mode

So now I have my Ctrl keybindings back and I can move forward happily with Terminal embedded within VS Code.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay