51

I just started using Visual Studio Code and am faced with an annoyance every time I paste in YAML code in an existing .yml file. Basically, the editor seems to auto-format the document and in doing so messes up the significant spaces in the document. This causes builds in Azure Devops to break. Although VS Code formats the document nicely into collapsible regions, the formatting annoyance makes it hard to use. Any help would be appreciated.

BEFORE:

BEFORE


AFTER: enter image description here

5 Answers 5

40

I fixed this by changing editor.autoIndent settings for yaml and dockercompose language

Update: In VsCode, press ctrl+shift+p (cmd+shift+p in Mac), and search for Preferences: Open User Settings (JSON) to add the following configuration:

"[yaml]": {
  "editor.autoIndent": "advanced"
},
"[dockercompose]": {
  "editor.autoIndent": "advanced"
},
"[github-actions-workflow]": {
  "editor.autoIndent": "advanced"
}
4
  • 2
    Point I'd add, modify the vscode settings.json Preferences: Open User Settings (JSON) file as in suggestion made by @tal-kohavy
    – Hillsie
    Commented Sep 17, 2022 at 4:02
  • 2
    For GitHub Actions users add the setting to "[github-actions-workflow]" as well.
    – xab
    Commented Oct 30, 2023 at 11:56
  • 1
    If using GitHub actions extension, also add "[github-actions-workflow]": { "editor.autoIndent": "advanced" }, Commented Apr 12 at 14:37
  • 1
    What does value "advanced" do here? I'd like to disable it entirely. Commented May 21 at 15:28
25

In VsCode, press ctrl+shift+p on Windows (or cmd+shift+p in Mac), and search for Preferences: Open User Settings (JSON). There, I added this line:

"[yaml]": {
  "editor.defaultFormatter": "redhat.vscode-yaml"
},

I picked it because it contained the word yaml, so I figured it must be yaml specific.
Anyhow, it seems to do a pretty good job for me.

2

Looks like the problem is in the first line. Maybe when you copy the code, you're not copying the indentation on the first line. One trick I use, is copying from the end of the preceding line, so the copied code starts with a newline, and then the paste is perfect.

Or just add the indentation on that first line, after pasting.

1

Turn off the setting format on paste. This is a global setting, but plugins sometimes have their own, so if you're running a formatter like prettier, you'll need to see whether this is even an option with that plugin.

1
  • 4
    The global setting is off, I have prettier installed and it doesn't seem to have any setting for this. Disabling Prettier didn't change the behavior Commented May 25, 2020 at 4:14
1

VSCode has a command to "Save without Formatting".

I noticed that my indentation changes were altered when I saved the YAML file. However, if I press cmd + shift + p in Mac, and type Save without formatting, and click that command to save, my indentations are preserved.

For Mac, the same command can also be executed with the keyboard shortcut cmd + K + S

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.