home about me

Simple workspace shortcuts

Having a few shortcuts to quickly pull up your different workspaces really makes a difference.

I have a lot of different projects I work on. Small ones, like this blog, just require me to open my editor and run a single console command. Bigger ones might involve a dozen docker containers and multiple folders full of git repositories and webpack tasks. Juggling many clients and hobby projects, it quickly becomes tedious to switch between projects - in fact, switching will often take more time than making the changes you set out to do!

A fast solution to ease this pain is setting up a single command to bring up each workspace. The techniques I mention are possible whatever your setup is, but I am running iTerm2 with zsh on Mac OS.

The first thing you want to do is making sure your projects are able to run side by side. This cuts down on the overhead of having to close the project you’re currently working on just to make a few changes on a different project, and will save you a lot of time and energy in the long run.

The main culprit here are ports. Only a single process can listen to one port, so you should set up each project in a way that the port numbers used on your machine are not in conflict with each other.
Most build tools support doing so via some variation of a --port command line parameter. You can add parameters to your npm command by adding a double dash --, so npm run dev would become npm run dev -- -p 8080.
For docker, you can override the relevant configurations.

If you can’t do that, it’s not the end of the world. You could build yourself helpers to quickly close the other projects you have open. For docker you could use docker ps -aq | xargs docker stop to stop all running containers, for desktop applications you can send a shutdown command via osascript -e 'quit app "Visual Studio Code"'. If that doesn’t work for some reason, you have the (somewhat dangerous) possibility of using pkill and killall.

I suggest doing this setup one by one, the next time you open each project’s workspace. Take note what programs you need to open and which commands you need to run, chain them, and put them in a shell alias or bash file.

For my blog, this alias is really simple. My .zshrc file contains an entry for a goblog command:
alias goblog='cd /Users/reneroth/projects/reneroth.xyz-jekyll-blog && code . && npm run dev -- -o'
It opens the project folder, open Visual Studio Code, and runs the npm command needed to build and serve the project. The -o parameter gets passed through and automatically opens the browser.

For bigger projects, the script can get more detailed:

docker ps -aq | xargs docker stop # stop all runnnig docker containers
open -a "Sequel Ace" # open database client
cd ~/customer/project/backend # open backend folder
code . # open backend in VS Code
docker-compose up -d # bring up backend containers
open -a "Google Chrome" http://backend.test/ # open the backend in the browser
cd ../frontend # open frontend folder
code . # open frontend in VS Code
npm run dev -- -o # run frontend pipeline, opening the browser

If your project is one of those behemoths where you need more than one terminal open and running at the same time, all those techniques can be combined with any scripting capabilities your terminal offers. For iTerm2, I use the neat iterm-workspace script in combination with the techniques mentioned here.