Controlling VSCode from the Terminal


reading time 4 min

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?

In this article I’ll show you some of my fav tips for controlling VSCode from the command line.

“Install” the VSCode command line tool

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.

The file is: /Applications/Visual Studio Code.app/Contents/Resources/app/bin/code

That’s quite a mouthful. Plus, its a filepath with a space in it. Ugh. So un-unixy.

Anyway, you can make it easier to access by making a symlink to it:

This command will make a symlink from your /usr/local/bin directory to it. You can put it anywhere that’s in your $PATH.

1
$ 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.

Start VSCode from the command lien

Now that you have easy access to the command, let’s use it.

Start VS Code, or start it and open myrepo

1
2
$ code
$ 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.

Open a file

Give code a filename and it will open that file. If that file is already open, it will bring that window to the front.

1
2
$ cd ~/git/myrepo
$ code main.go

Open a file at a particular line

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?

With the -g flag, VSCode opens the file and moves to the position specified. The man page explains it like this:

1
2
  -g --goto <file:line[:character]>          Open a file at the path on the specified line and character
                                             position.

Here’s an example of me copying and pasting the filename and position:

1
2
3
4
$ go build
# github.com/DNSControl/dnscontrol/v4/models
models/record.go:682:5: syntax error: unexpected comma at end of statement
$ 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.

The -g flag is so useful I’m surprised it isn’t the default, with a --no-goto option to disable it.

Create some aliases to save keystrokes

These aliases work in zsh, bash, and other shells modeled after the Bourne Shell (/bin/sh).

1
2
alias c='code -g'   # Open a list of files.
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?

1
2
3
4
5
6
7
$ c                         Start VSCode
$ c file.go                 Open file.go
$ c file.go:10              Open file.go and move to the 10th line
$ c file.go:10:             Open file.go and move to the 10th line
$ c file.go:10:4            Open file.go and move to the 10th line, position 4
$ c one.go two.go thee.go   Open all three files
$ 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.

1
2
3
4
$ g                         Start VSCode
$ g file.go                 Open file.go
$ g file.go:10 file2.go     Still only opens file.go
$ 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.

1
$ 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. 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

Wait, there’s more!

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!

P.S. Know any hugo template designers?

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!




Tom Limoncelli

Tom Limoncelli

Recent Posts


  1. Non-developers are writing apps
  2. AI-Driven SRE Tools. Please stop.
  3. A Flag for the Anti-Authoritarian Movement
  4. Make VSCode ‘active tab’ more visible
  5. Controlling VSCode from the Terminal

Archive


Categories


Tags


I agree that this website may store my data to personalize my journey in accordance with their Terms & conditions

Powered by Hugo | Theme - YesThatTheme © 2017 - 2026 Tom Limoncelli