← Technical

TIL: Setting Up Tmux in VS Code

I like to use tmux in the terminal, even in VS Code. However, I wanted to have a single tmux session for each VS Code workspace, because otherwise I have to switch working directory every time I open a new project. So here’s how I got that set up!

First up, in the VS Code settings:

  "terminal.integrated.profiles.osx": {
    "bash": null,
    "zsh": null,
    "fish": {
      "path": "/opt/homebrew/bin/fish",
      "args": ["-l"],
      "icon": "terminal-tmux"
    }
  },
  "terminal.integrated.env.osx": {
    "VSCODE_WORKSPACE": "${workspaceFolderBasename}"
  },

The first bit creates an integrated terminal profile for fish, the shell I use, which just launches it normally. The second bit adds an environment variable on launch, VSCODE_WORKSPACE, which inserts the title of the VS Code workspace via the workspaceFolderBasename VS Code variable. I set null for the default bash and zsh profiles so they don’t show up in VS Code.

I actually launch tmux from the standard fish initialization script, config.fish:

if status is-interactive
    if not set -q TMUX
        if set -q VSCODE_WORKSPACE
            exec tmux new -A -t "$VSCODE_WORKSPACE"
        else
            exec tmux new -A -t default
        end
    end
end

A few parts: