24

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
GollyJer
  • 23,857
  • 16
  • 106
  • 174

5 Answers5

43

Add this line to your settings.json file.

"[markdown]": {
    "files.trimTrailingWhitespace": false
}
GollyJer
  • 23,857
  • 16
  • 106
  • 174
3

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
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yashu Mittal
  • 1,478
  • 3
  • 14
  • 32
2

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.

https://github.com/microsoft/vscode-docs/raw/vnext/release-notes/images/1_68/settings-editor-language-specific-default.gif

(Theme Light Pink)

This example above is for line wrapping, but you can adapt it to reference trim_trailing_whitespace.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

Do Ctrl-K s. This will "Save without formatting", which also means, without trimming trailing whitespace in the file you're editing

kolypto
  • 31,774
  • 17
  • 105
  • 99
0

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
Mathias Asberg
  • 3,562
  • 6
  • 30
  • 46