Reputation: 733
I want to avoid auto indent behaviour when I save a (javascript) file in Visual Studio Code without turning off complety auto indentation.
I have this code (and I would like to keep this indentation if possible):
const $tab_a = $('tab_a')
const $tab_b = $('tab_b')
;[$tab_a, $tab_b].forEach($e => $e.onclick = () => {
if ($e.className) return
$tab_a.classList.toggle('active')
$tab_b.classList.toggle('active')
})
However, every time I save with Visual Studio Code it changes indentation like this:
const $tab_a = $('tab_a')
const $tab_b = $('tab_b')
;[$tab_a, $tab_b].forEach($e => $e.onclick = () => {
if ($e.className) return
$tab_a.classList.toggle('active')
$tab_b.classList.toggle('active')
})
I tried different settings in Visual Studio Code:
Unfortunatly any of them worked for me.
Any help will be really appreciate it.
Note: I'm not using extensions.
Upvotes: 8
Views: 12738
Reputation: 125
check setting.json (ctrl+shift+p then choose 'Preferences: Open Settings (Json)' ) for:
"editor.formatOnSave": true/flase
it should be set by default to false but maybe some extension toggled to true so just change it back to false.
Hope that helps
Upvotes: 4
Reputation: 1653
I got the same problem, when I hit save for my js file it was indented with 4 spaces instead of 2.
I found this github thread File reformatting to 4 spaces for indentation on save. #28327
Then I searched for JS-CSS-HTML Formatter extension, which was installed, so I proceeded to configure it:
Press F1, enter Formatter Config, open the config file:
It will open the settings in a new tab and you just need to adjust the option indent_size from 4 to 2
Upvotes: 1
Reputation: 1075735
To turn off formatting on save:
That sets the "editor.formatOnSave"
setting to false
in settings.json
.
When I did that, your example was left alone when I saved the file. (Whereas with the setting on, it gets reformatted as you describe in the question when I save it.)
Upvotes: 10