Joshua's Cheatsheets - VSCode Settings, Power User Tips, and Misc Notes
Light
help

Opening Via CLI

Opening Via CLI - Windows

VSCode is exposed, via the system PATH, as code. This allows you to do the following, in a standard command prompt:

  • Open a directory: code {dir}

    • You can use code . for opening the current directory
  • Open a specific file code {filePath}

If you notice that every time you open a file with this method, it also opens a file called cli.js, there is likely a permission mismatch; e.g. you set Code.exe to always open as an Administrator, but the console you called code from was not launched as an administrator.

  • The fix here is to simply use an elevated command prompt, or remove the setting that code.exe always run with Administrator privileges.
  • Relevant issues: #72521, #91613

Troubleshooting

  • Stale path / environment variables in terminal

    • Try completely restarting VSCode - close all windows and relaunch
    • Try running VSCode as an administrator; this can often fix path expansion issues
    • Check which shell you are using as the intergrated terminal

      • You can override with settings.json -> terminal.integrated.shell.{OS}
  • TypeScript import autocomplete keeps using absolute or incorrect relative paths

    • Try setting settings.json -> "typescript.preferences.importModuleSpecifier": "relative"

Debugging / Launching

Launch.json Tips

  • For Yarn, set runtimeExecutable to "yarn", and runtimeArgs should just be an array of the commands you would normally type after yarn run. E.g:

    {
    	"runtimeExecutable": "yarn",
    	"runtimeArgs": ["debug-build"],
    }
  • If you have breakpoints for all "Uncaught Exceptions" on, you might want to exclude node_modules, or any other third-party directory, from that rule (if you are seeing a bunch of exceptions you don't care about).

    • You can do this with the skipFiles feature.
    • Example: "skipFiles": ["<node_internals>/**", "${workspaceFolder}/node_modules/**/*.js"]
  • Viewing the results in the integrated console

    • Use "console": "integratedTerminal"

Strange Issue with Auto-Attach

I've noticed a strange issue with auto-attach in VSCode. Even if I have it turned on, it (the VSCode debugger) often will not work if Chrome is open, although the auto-attacher in Chrome works just fine. It's almost like Chrome is hijacking the debugger port or something.

The fix for me was to manually create a launch.json entry, rather than relying on only the --inspect-brk flag for Node. In addition, make sure the entry has type: "node", not type: "chrome".

Settings

For my personal settings, see /settings.

Disable annoying word autocomplete

{
	"editor.wordBasedSuggestions": false,
	"javascript.suggest.names": false
}

Setup basic code formatting rules

{
	"editor.tabSize": 4
}

Disable CRLF (\r\n) endings on Windows

{
	"files.eol": "\n"
}

Force indent style

If you really want to ensure a specific indent style in a shared repo, this is a forceful way to do so, in your .vscode/settings.json:

{
	"editor.insertSpaces": true,
	"editor.tabSize": 4,
	"editor.detectIndentation": false,
	"editor.formatOnSave": true,
	"editor.autoIndent": "full"
}

Extensions:

Per workspace settings

You can control settings per root directory (workspace/project) through {WORKSPACE_ROOT}/.vscode/settings.json.

  • This file will be automatically created if you touch a workspace level setting (through the GUI), but you can also manually create it:

    • mkdir .vscode && touch .vscode/settings.json

You can add a file (extensions.json) to your workspace .vscode folder, so that when other devs checkout your code, VSCode will recommend for them to install the extensions that will help them the most. Read more here.

Snippets

Main doc: User Defined Snippets

There is a lot in this doc, so make sure to read carefully!

Advanced Transforms

  • Examples:

    • Official docs: here
  • Escaping: It's complicated!

  • Combining tab-stops with variables

    • You can combine tab-stops (where the cursor tabs between) and variable / macros, by nesting:
    • Example: "src=\"${1:${CLIPBOARD:IMAGE_SOURCE}}\"

Javascript Type Safety

There are are a bunch of options for getting some type-safety with JS in VScode, and better intellisense. Of course, you are free to also use linters, like ESLint (see my JS DevOps Cheatsheet), but that is not the only option. There is actually built-in type checking options!

To get the full power of this feature, you really should use JSDoc comments to provide additional type annotations, control inferred types, and so on. This is a hefty topic, so I've moved all my notes on it to a dedicated JSDoc cheatsheet page, under this section.


Coming from Notepad++ / "where is X?"

If you are coming from using Notepad++, or a vastly different IDE, you might be looking for certain things. For example:

  • "Show all characters"

    • In Notepad++, this shows whitespace characters (\t, \s, etc.), as well as EOL (end of line) characters (\r or \n). I have not a single method to do both on VSCode, but you can do them separately.

      • Whitespace: Menu -> View -> Render Whitespace
      • EOL: There is an extension that shows them

Searching

  • You can use regex
  • You can filter your search by specific files and by directory

    • See guide here
    • Uses glob syntax
    • Use , to separate multiple patterns
    • Use ! to negate a pattern
  • You can exclude files and directories

    • Inherits settings from .gitignore and global settings

Linting / Formatting Controls

Built-in

First, I want to point out that you can customize settings per language in VSCode, no extension required. Just put the language specifier as the JSON key, and then the settings as the object value. Like so:

{
	"editor.tabSize": 4,
	"[php]": {
		"editor.tabSize": 2
	}
}

You can even specify certain actions to be ran after certain file types are saved, by using editor.codeActionsOnSave under a language specifier!

If you are have multiple language formatting extensions installed, and want to tell VSCode which one to use per language, that is another thing you can do with settings:

{
	"[php]": {
		"editor.defaultFormatter": "vscode.php-language-features"
	}
}

JS Linting

Check out my notes here.


Building an extension

For right now, my only public notes on this are on my blog: https://joshuatz.com/posts/2019/building-a-vscode-extension-some-tips-and-tricks/

Markdown Source Last Updated:
Thu May 21 2020 23:45:35 GMT+0000 (Coordinated Universal Time)
Markdown Source Created:
Mon Aug 26 2019 07:49:42 GMT+0000 (Coordinated Universal Time)
© 2020 Joshua Tzucker, Built with Gatsby