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
Updated 9/21/2021 with new syntax
Add the below snippet to your remote settings in VSCode.
{
"terminal.integrated.profiles.linux": {
"tmux": {
"path": "/usr/bin/tmux",
"args": [
"new-session",
"-A",
"-s",
"main"
],
},
},
"terminal.integrated.defaultProfile.linux": "tmux",
}
Session Per Workspace
Update 9/21/2021: Looks like config variables have been fixed! Updated syntax and variables below.
If you want one tmux-session per project, you can use vscode variables
Add the below snippet to your remote settings in VSCode.
{
"terminal.integrated.profiles.linux": {
"tmux": {
"path": "/usr/bin/tmux",
"args": [
"new-session",
"-A",
"-s",
"vscode-${workspaceFolderBasename}"
],
},
},
"terminal.integrated.defaultProfile.linux": "tmux",
}
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.