Controlling VSCode from the Terminal<br>About me
Controlling VSCode from the Terminal<br>Jun 1, 2026
reading time 4 min
In this article<br>[Show]
Do you prefer the command line over the mouse? Would you rather start VS Code from the Terminal than finding it in Applications and clicking around to open your repo?<br>In this article I’ll show you some of my fav tips for controlling VSCode from the command line.<br>“Install” the VSCode command line tool<br>VSCode comes with a command-line tool called “code”. There’s nothing to install. The problem is that it probably isn’t in your $PATH.<br>The file is: /Applications/Visual Studio Code.app/Contents/Resources/app/bin/code<br>That’s quite a mouthful. Plus, its a filepath with a space in it. Ugh. So un-unixy.<br>Anyway, you can make it easier to access by making a symlink to it:<br>This command will make a symlink from your /usr/local/bin directory to it. You can put it anywhere that’s in your $PATH.<br>$ ln -fs '/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code' /usr/local/bin/code
You need to quote the destination to deal with the fact that the filename has… ugh… spaces in it.<br>Start VSCode from the command lien<br>Now that you have easy access to the command, let’s use it.<br>Start VS Code, or start it and open myrepo<br>$ code<br>$ code ~/git/myrepo
It’s important to point out that if VSCode is already running, it won’t start more. The tool sends a message to the running VSCode passing along your command line parameters. More technically, it is a client that talks to a running VSCode and starts one if it isn’t already running.<br>Open a file<br>Give code a filename and it will open that file. If that file is already open, it will bring that window to the front.<br>$ cd ~/git/myrepo<br>$ code main.go
Open a file at a particular line<br>Your compiler tells you there is an error at a particular line and column. Why scroll when you can paste the filename and position info the compiler gives you?<br>With the -g flag, VSCode opens the file and moves to the position specified. The man page explains it like this:<br>-g --goto Open a file at the path on the specified line and character<br>position.
Here’s an example of me copying and pasting the filename and position:<br>$ go build<br># github.com/DNSControl/dnscontrol/v4/models<br>models/record.go:682:5: syntax error: unexpected comma at end of statement<br>$ code -g models/record.go:682:5:
Pasting the filename along with the position has saved me hours. It’s not just that I don’t have to scroll to the right line, but being placed at the right character position is downright luxurious.<br>The -g flag is so useful I’m surprised it isn’t the default, with a --no-goto option to disable it.<br>Create some aliases to save keystrokes<br>These aliases work in zsh, bash, and other shells modeled after the Bourne Shell (/bin/sh).<br>alias c='code -g' # Open a list of files.<br>function g () { code -g "$1"; } # Open the first file, ignore the other args.
c is an alias for code -g – It takes a list of filenames. This saves me both time and keystrokes. Did I mention I thought -g should be the default?<br>$ c Start VSCode<br>$ c file.go Open file.go<br>$ c file.go:10 Open file.go and move to the 10th line<br>$ c file.go:10: Open file.go and move to the 10th line<br>$ c file.go:10:4 Open file.go and move to the 10th line, position 4<br>$ c one.go two.go thee.go Open all three files<br>$ c *.go Open all "*.go" files in this folder
g is an alias (technically a function) that is similar to c but only processes the first file.<br>$ g Start VSCode<br>$ g file.go Open file.go<br>$ g file.go:10 file2.go Still only opens file.go<br>$ g file.go:10 *.go Still only opens file.go
Why would someone want this? Because now we can be extra lazy with our copying and pasting. This command will only open models/record.go and ignore the rest of the line.<br>$ g models/record.go:682:5: syntax error: unexpected comma at end of statement
Now I can double-click the line instead of surgically selecting just the first part of the line.<br>Of course, this doesn’t work if the line has shell chars like parens, semicolons, and so on. However that’s rare and easy to spot, at least for me. YMMV<br>Wait, there’s more!<br>code -h will print a manual page including dozens of other flags. You can use --diff to open two files and diff them. You can add folders, install extensions, add MPC, and more!<br>P.S. Know any hugo template designers?<br>The template I’m using for this blog is quite boring. If you have design skills, a deep understanding of color and layout, and understand Hugo, please reach out to me at tal at whatexit dot org. Thanks!
Please enable JavaScript to view the<br>comments powered by Disqus.<br>Tom Limoncelli
Recent Posts<br>Non-developers are writing apps<br>AI-Driven SRE Tools. Please stop.<br>A Flag for the Anti-Authoritarian Movement<br>Make VSCode ‘active tab’ more visible<br>Controlling VSCode from the...