I generally have the Trim Trailing Whitespace setting turned on in VSCode because I want that behaviour for code files.
However, I have a folder of text resources (.txt files) that I would like to exclude from this rule. Some of the text files need trailing whitespace on the lines.
Is it possible to selectively turn this rule on/off based on file path or file extension?
I've tried having a .vscode/settings.json file at the root of my workspace turning the feature on, and another in the folder containing the text resources to turn it off and that doesn't work.
Is there an extension that enables this? I looked but wasn't able to find one.
In settings.json, insert:
"[plaintext]": {
"files.trimTrailingWhitespace": false
}
Related
Currently, whenever I create a new file (no matter the file type) it starts out using spaces for indention. I always have to click and convert to tabs.
How can I configure all new files to use tabs by default to avoid this step?
Put the following in whichever settings file makes sense for you (your user settings.json, or a .vscode/settings.json, or a .code-workspace file):
"editor.insertSpaces": false
You can change this setting on a per-file-type basis like so:
"[json]": {"editor.insertSpaces": true},
"[javascript]": {"editor.insertSpaces": false}
To change the indentation for an existing file, use the Convert Indentation to Tabs command.
You can also use editor.detectIndentation to change whether VS Code should try to detect what indentation a file is already using to follow it.
I know about workspace settings. My issue is that workspace settings also apply to any files that are opened not in that workspace. Example:
"editor.formatOnSave": true // put this in some workspace settings.
open a file not in that workspace (maybe in a different project) in the same window. This happens when I type $ code ~/someFile.txt
When I save that file, it gets formatted when it shouldn't.
Any workarounds?
Specifically for saving a file without formatting, try new command for saving without formatting. Just added in v1.28.0:
The new command Save without Formatting
(workbench.action.files.saveWithoutFormatting) can be used to save a
file without triggering any of the save participants (for example,
formatters, remove trailing whitespace, final newline). The default
keybinding is Ctrl+K S. This is useful when editing files outside your
normal projects, which may have different formatting conventions.
In Visual Studio Code the setting
"files.trimTrailingWhitespace": true
removes trailing white space when files are saved, or Shift + Alt + F is used to format a file, but this breaks Markdown formatting.
How do you selectively turn off white space trimming for Markdown?
Add this line to your settings.json file.
"[markdown]": {
"files.trimTrailingWhitespace": false
}
You can use EditorConfig by adding .editorconfig at the root of your project:
[!markdown]
trim_trailing_whitespace: false
Or as GollyJer suggested, add this code snippet in the settings.json file:
"[markdown]": {
"files.trimTrailingWhitespace": false
}
You can now (VSCode 1.68, May 2022) do this through the Settings GUI instead of directly your settings.json.
Settings editor improvements
The Settings editor now shows a default value override indicator for language-specific settings.
As a note, one can view language-specific settings by adding a language filter in the Settings editor search bar, and one can add such a filter either by typing it out explicitly, or by clicking the filter button on the right of the search bar, and selecting the "Language" option.
When the default value override indicator shows up, it indicates that the default value of the language-specific setting has been overridden by an extension. The indicator also indicates which extension overrode the default value.
(Theme Light Pink)
This example above is for line wrapping, but you can adapt it to reference trim_trailing_whitespace.
Do Ctrl-K s. This will "Save without formatting", which also means, without trimming trailing whitespace in the file you're editing
Solution
Add or update .editorconfig in root of your project and add the 2 following lines to prevent trimming on whitespace in VScode on matching file extension's
Update .editorconfig with following
[*.md]
trim_trailing_whitespace = false
[*.mdx]
trim_trailing_whitespace = false
I have a markdown file which I would like to permanently highlight using JSON syntax.
But I don't want to treat ALL .md files this way, just a few of them.
Is there a way to accomplish that, so I don't have to manually change the highlighting each time I open the file?
You can change this with the 'files.associations' setting. For example
"files.associations": {
"*.md": "json"
}
The question tells everything.
I need to know how to set up my NetBeans to not save whitespaces in a PHP file. I heard this is possible, but I can't find how to do it.
Thanks!
You can remove trailing whitespaces on save. To do so, go to Tools->Options->Editor->On Save, select the language you want (or All) and there is an option called "Remove Trailing whitespaces from" with possible values: None, All lines, Modified lines
And next time you save file, it will remove trailing whitespaces.