Easiest way to add syntax highlighting for Vue.js components in Visual Studio Code without installing any extensions.htt.
While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or edited it to ensure you have an error-free learning experience. It's on our list, and we're working on it! You can help us out by using the 'report an issue' button at the bottom of the tutorial.
Through this tool, we’ll know whether we are using the correct formatting for the project, whether the braces are in the right place, whether or not there is a semicolon at the end of the line, whether there is an unused import, among others.
For ESLint to work properly, you must configure it. In addition, you need to have some packages installed.
This configuration can be complex, so we can use a template that already has all we need. Let’s use the vue-cli to create a project using the webpack template. With Node 8 or higher installed, run:
If you haven’t heard about npx yet, now is the time to learn about it! It’s a Node package runner, responsible for running the Node packages, without the need to install it globally. In other words, you don’t need to run
npm install -g <package>.
After the project was created, let’s execute the following commands:
npm install command will install all required packages, including packages that are in the devDependencies. The
code . command will open Visual Studio Code in the current directory.
When you’re finished installing and are now in Visual Studio Code , open the
src/App.vue file. You will notice that there’s no syntax highlighting, as is shown in the following figure:
Visual Studio Code will warn you, in the lower right corner, that there are extensions for Vue. Here are some good extensions to start with:
Visual Studio Code Vue Template
- Vue 2 Snippets
- Vue Peek
- Editorconfig for VSCode
After installing these extensions and restarting VSCode, we have syntax highlighting, see:
For ESLint to work correctly, you must change the VSCode preferences. Go to
File > Preferences > Settings and edit the User Settings file, adding the following configuration:
ctrl+alt+f on Windows or
ctrl+shift+i on Linux or
ctrl+options+f on Mac OS to perform the formatting of the code. ESLint will validate the code and display some errors on the screen.
These errors can be corrected automatically, and it’s not necessary to correct each error manually. To do this, you can press
ctrl+shift+p and select
ESLint: Fix all problems:
We can still optimize ESLint by configuring it to perform code formatting every time we save the file. To do this, add the following configuration:
eslint.autoFixOnSave, enables auto fixing on file save. You must restart Visual Studio Code to apply this change.
Visual Studio Code Vue Formatter
We still have a problem that occurs between formatting a document and saving it. This happens because ESLint is not running when we format the document. This next screenshot shows the problem:
As you can see from that image, we execute alternately the command to format the code (Format Code) and to save it. The command to format code is not using ESLint yet, it uses VSCode’s own formatter (or another like Prettier). Now, when VSCode saves the file, ESLint will be executed, thanks to
To solve this problem we need some additional settings regarding the Vetur extension. These are:
Visual Studio Vue Cli
With these three settings, we now have the correct configuration for ESLint to fix the errors automatically for you 🤓. In this way, we can write code and, when there is some formatting errors, ESLint will automatically fix them.
Vscode Vue Highlight
Here’s the complete list of settings for the configuration file 💪: