haki
haki

Reputation: 9779

How to format svg and xml like html in vscode?

When I open an svg or xml file and try to run "Format Document" I get the message:

There is no document formatter for 'xml'-files installed.

To overcome that I usually just do "Language Mode" -> select HTML -> "Format Document".

Is it possible to use the HTML formatter for XML and SVG?

P.S I rather not install extensions.

Upvotes: 16

Views: 22861

Answers (3)

Adel Benyahia
Adel Benyahia

Reputation: 429

I will suggest using the svgo VSCode extension:

Screenshot of VS Code with a test.svg file opened. The context menu is opened showing these options: 1. Minify current SVG file, and; 2. Prettify current SVG file.

Upvotes: 0

mosaic
mosaic

Reputation: 129

Sometimes when you open an SVG file in vscode all the content is on a single line. If you just want to do a basic reformat for visual inspection or printing, you can generate a reformatted file using PowerShell, like so:

$svgOriginal = 'C:\yourfolder\yourfile.svg'
$svgFormatted = 'C:\yourfolder\yourfile.txt'
Get-Content $svgOriginal | format-xml | out-file $svgFormatted -Encoding utf8

Open the output file in vscode, set the language mode to HTML and (if necessary) turn on word wrap (Alt + Z)

Upvotes: 0

syntaqx
syntaqx

Reputation: 2876

You should be able to override any extension to the language of your choice:

"files.associations": {
    "*.xml": "html",
    "*.svg": "html",
}

Chuck that in your settings.json and you should be golden (although, I recommend not doing this, but it's your life)

Upvotes: 33

Related Questions