Skip to content

Visual Studio Code - setting it up for work

Visual Studio Code (VS Code for short) is Microsoft's free code editor, and today the most popular one among developers. It runs on Windows, macOS and Linux, opens quickly, and extensions turn it into an environment for almost any language. It works straight after installing, but fifteen minutes on settings pays for itself in your first week.

In short

  • A few settings to start with: format on save, word wrap, trimming stray spaces, an empty line at the end of each file.
  • Install extensions for the project, not just in case. Each one is code running on your computer.
  • Five shortcuts change everything: Ctrl+P, Ctrl+Shift+P, Ctrl+Shift+F, Ctrl+D and F2.

Editor or IDE?

The choice used to be simple: a light text editor, or a heavy IDE (integrated development environment) that compiles, debugs and suggests by itself. VS Code sits in between. On its own it is a fast editor, and extensions add suggestions, debugging or database support - only where you need them. For PHP, JavaScript, Python or editing configuration files it is plenty. For big Java or C# projects, many people still choose a full IDE.

Settings to start with

Open the settings with Ctrl+,. It is handier to edit them as a file, though: Ctrl+Shift+P, type "Open User Settings (JSON)" and paste:

{
  "editor.formatOnSave": true,
  "editor.wordWrap": "on",
  "editor.rulers": [120],
  "editor.minimap.enabled": false,
  "files.trimTrailingWhitespace": true,
  "files.insertFinalNewline": true,
  "files.autoSave": "onFocusChange",
  "git.autofetch": true
}

In order: format code on save, wrap long lines, show a vertical line at character 120, hide the file minimap on the right, trim spaces at the ends of lines, add an empty line at the end of each file, save a file when you switch to another window, and check in the background for new changes in the repository. If an error slips into that JSON, the JSON formatter will find it.

Extensions: less is more

The extensions panel opens with Ctrl+Shift+X. It is tempting to install everything from "50 must-have extensions" lists. Do not. Every extension slows down the editor's start, can clash with others and - more importantly - is code with access to your files. Install from trusted publishers, and only what you use.

A good starter set:

  • support for your language (PHP Intelephense, Python and so on) - VS Code usually suggests it when you open a file,
  • a formatter and a linter for the project, such as Prettier and ESLint for JavaScript - they keep the style in check so you do not have to,
  • EditorConfig - reads the project's .editorconfig, so indentation and line endings are the same for everyone,
  • Docker or Remote - SSH if you work with containers or on a server - you edit remote files as if they were on your disk.

Git is built in: the Ctrl+Shift+G panel shows your changes and lets you stage files, commit and compare versions. What that looks like day to day is in Everyday work with Git.

Shortcuts that change everything

  • Ctrl+P - open a file by typing part of its name. Faster than clicking through the folder tree.
  • Ctrl+Shift+P - the command palette. Every editor feature, by name. If you remember one shortcut, make it this one.
  • Ctrl+Shift+F - search (and replace) across the whole project.
  • Ctrl+D - select the next occurrence of a word and edit them all at once. Alt+click adds a cursor anywhere.
  • F2 - rename a variable or function everywhere it is used (if the language extension supports it).
  • Ctrl+` - the terminal built into the editor.
  • Ctrl+G - go to a line number, from an error message for instance.

On macOS, use Cmd instead of Ctrl.

Settings for a project and a team

The settings above are yours and apply everywhere. A project can have its own too, in a .vscode/settings.json file in the repository - a different indent size, say, or the formatter switched off for some files. Next to it you can add .vscode/extensions.json with recommended extensions: anyone who opens the project is offered to install them. That way a new person on the team has the same setup in five minutes.

Your own settings move to another computer with Settings Sync (the account icon in the bottom left).

What about an AI assistant?

VS Code has a built-in place for AI assistants that suggest code and can even make changes across many files on their own. They can speed up your work a lot, but keep two things in mind: read every change before you accept it, and check whether you are allowed to send company code to an outside service (more in AI and privacy). And if you are still learning, see How to learn programming.

Tips

  • Open a folder, not files (File > Open Folder). Only then do project search, Git and project settings work.
  • Zen mode (Ctrl+K, then Z) hides everything except the code. Good for focused writing.
  • Comparing two files: select both in the tree with Ctrl and choose "Compare Selected".
  • Editor lagging? Ctrl+Shift+P > "Start Extension Bisect": VS Code finds the extension that slows it down by itself.
Advertisement