简体   繁体   中英

Disable onsave formating for .ejs files in visual studio code

I'm working with ejs files, but in order to reuse some code, I'm using the includes feature. Given that some opening/closing html tags are placed in other files, after I save my changes, something (I don't know if prettier extension or VS code editor) is including the closing tags into my code, causing several problems when I running it.

An other solution is to use a .prettierignore file and put in:

*.ejs

I don't use EJS myself, but after playing around with it in VSCode, I noticed that my .ejs files were being treated as HTML files. You can see how your VSCode is interpreting the file by looking in the bottom right corner of the editor. You could search for EJS extensions as well.

You likely have the "Editor: format on save" option enabled. To disable that setting, go to Preferences, and in the search bar type "format on save". Find the setting. Uncheck the box. That should fix the problem.

If, as you mentioned, your closing HTML tags are in other files, then you have invalid HTML and the formatter (both Prettier's and the default one) will autocomplete the closing tag. (I would wager EJS also wouldn't like the lack of closing tags, but since I don't use it I'm not so sure, maybe it's fine.)

在此处输入图像描述

in your settings.json you should add these settings. just instead of "[css]" type what you want. (the format of the file)

and just please ignore line 2:) that's not related to this answer.

Edit

as @aegatlin said use this: "[html]" .(if "[ejs]" didn't work for you)

Zulhilmi Zainudin has the solution https://medium.com/@zulhhandyplast/how-to-disable-vs-code-formatonsave-for-specific-file-extensions-c60e8f254243

In vscode setting file, associate ejs extentions files to a « language ». Then you can specify different rules for this that language:

.vscode/settings.json file content:

{
    "files.associations": {
        "*.ejs": "ejs" // this create the language « ejs » which refers to any .ejs file
    },
    "[ejs]": { // custom settings for the language « ejs »
        "editor.formatOnSave": false
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM