How to open a new project in the same VS Code window
- Published at
- Updated at
- Reading time
- 2min
I have many aliases and shell shortcuts defined in my dotfiles. Generally, my setup works well, and I rarely tweak it or add new aliases.
But today's the day to add a new command alias!
If you're a VS Code user, you can set up and use the code
CLI command to open files/folders, diff files, or merge stuff. It's pretty neat, but I really just use it to open projects. Nothing fancy.
# In the morning ...
cd ~/projects/project-a
code .
# After lunch ...
cd ~/projects/project-b
code .
# you get the idea ...
There's nothing particularly wrong with this workflow except that code
will open a new VS Code window whenever you call it. If you don't close the previous window, you'll open more and more editors. The more projects you have, the more coding windows you have to juggle (or close).
During a typical workday, I quickly reach six or more open editor windows (because, of course, I don't close the windows and just fire up another code
command). Only after reading Nick Taylors' "One Tip a Week" newsletter I realized how much all these windows annoy me.
Nick, of course, had a solution: code -r
. That's it!
The -r
option will make VS Code open a new project and reuse the open window. No more new windows!
But of course, nobody wants to type the -r
option all the time, so here's my new alias following Nick's recommendation:
# reuse the existing window instead of opening another one
alias c="code -r"
And let me tell you, this alias (👇) already made it into my muscle memory.
# In the morning ...
cd ~/projects/project-a
c .
# After lunch ...
cd ~/projects/project-a
c .
Thanks, Nick!
Join 5.5k readers and learn something new every week with Web Weekly.