Article Image
read

I've been moving around a bit lately (Nowhere far... since COVID-19 and all), and have been leaning heavily on VS Code's Remote SSH Plugin. My network connection has been spotty, and have found that even though it's pretty good at not losing any of your code changes, that when I want to depend on the terminal, the session is not persistent. This is especially bad when I lose connection when running a dev-build (that hangs around) because I have to find and kill the process before starting coding again.

TMux Session

Luckily, the solution is easy enough: tmux!

In addition to sessions, tmux provides many powerful features including terminal splitting, history, and more. I'll be focusing on the session, of course :)

First step is to quickly install tmux:

sudo apt install tmux

VSCode Config

The solution is to set the default terminal provider on the remote settings to be tmux, and have it attach to either an existing session named main, or create a new one if it doesn't exist. The config to do that is this:

Single Session

{
  "terminal.integrated.shell.linux": "/usr/bin/tmux",
  "terminal.integrated.shellArgs.linux": [
    "new-session",
    "-A",
    "-s",
    "main"
  ],
}

Session Per Workspace

If you want one tmux-session per project, you can use vscode variables

WARNING: (5/23/2021) In the most recent version of VS Code, per-workspace tmux has stopped working for me. It no longer seems to resolve variables inside shell configuration (in this case ${workspaceFolderBasename}) See work-around below.

{
  "terminal.integrated.shell.linux": "/usr/bin/tmux",
  "terminal.integrated.shellArgs.linux": [
    "new-session",
    "-A",
    "-s",
    "vscode-${workspaceFolderBasename}"
  ],
}

Session Per Workspace (Workaround 6/15/21)

The only workaround I was able to find is by having vscode execute a bash script that looks at $PWD to determine the workspace. This is set up like so:

Add this line to your config (Change your username)

{
  "terminal.integrated.shell.linux": "/home/chris/bin/code-tmux"
}

And here is the bash script, which names the tmux session based on the $PWD variable, which looks set by vscode:

#!/bin/bash
exec tmux new-session -A -s vscode-$(basename $PWD)

Persistent Port Forwarding

As a bonus, I like to add port-forwarding to default to the port I use for my development. This means there's one fewer steps to starting to get back into development after reconneting.

{
    "remote.SSH.defaultForwardedPorts": [
    {
      "localPort": 9002,
      "remotePort": 9002,
      "name": "SA"
    }
  ],
}

Conclusion

Hopefully these small tricks make your zero-to-60 time much shorter when setting a new coding session.

That's it! Happy coding.

Blog Logo

Christopher LaPointe


Published

Image

Chris LaPointe

Another site of Code

Back to Overview