I want to change tabs to whitespaces when I save the file.
I thought there would an option in the settings or at least an extension, but I was not able to see it.
I have seen many other posts for removing trim.trailingWhitespace, but that is not what I am looking for.
I am also using the conversion from tabs to whitespaces when pressing the Tab key. But that is, again, not my issue.
What I am looking for is to save the file and automatically change all the tabs to whitespaces, like Qt Creator does.
It is going to depend on the language. You need to install/setup a language-specific formatter and then enable the "editor.formatOnSave" setting, which will literally apply the formatter rules when saving files.
This answer is for Python and JavaScript because that's what I normally use.
For JavaScript, I use the Prettier extension.
(It has plugins for other languages but I've mainly used it for JS.)
Then add these to your settings.json:
// Set the default setting
"editor.formatOnSave": false,
// Then toggle depending on the language
"[javascript]": {
"editor.formatOnSave": true
},
By default, Prettier already provides some default formatting rules. But you can specify your own configuration file to specify your own (or a project-specific) set of formatting rules.
.
├── ...
├── .prettierrc.js
├── test.js
...
└── <<other files>>
In .prettierrc.js:
// prettier.config.js or .prettierrc.js
module.exports = {
useTabs: false,
tabWidth: 4
};
That Prettier config specifies not to use tabs and use an indentation level of 4 spaces. Now, with that setup, when you save a file, it will automatically change tabs to whitespaces (which is what I understand is what you want). There are also other formatting options.
You'll know the extension is working because it shows "Prettier" in the status bar:
For Python, VS Code currently supports 3 formatting providers):
"autopep8"
"yapf"
"black".
I use "autopep8".
Install autopep8 on your environment. Then in VS Code, make sure to select the environment that has autopep8. Then add this to your settings.json:
// Set the default setting
"editor.formatOnSave": false,
"[python]": {
"editor.formatOnSave": true
},
"python.formatting.provider": "autopep8",
"python.formatting.autopep8Args": [
// "--ignore=W191, E101, E111" // Uncomment to disable fixing indentation
],
Here, autopep8 formats code to follow the PEP8 style guide, which already recommends spaces over tabs. So all that needs to be done is enable it.
You might also be interested in VS Code settings related to spaces (so that tabs will not be put into the file in the first place):
"editor.detectIndentation": false,
"editor.insertSpaces": true,
"editor.tabSize": 4,
Whenever I face an indentation problem, I go online and google "beautify python code" and the 2nd or 3rd link of tutorials-point helps me out( link ).
Just put in your code and it will (in most cases) completely fix it for you or guide you theough the errors in a better way than an interpreter.
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.
In the VS code editor, the default setting is to replace tabs by spaces, which is what I want. However, this is disastrous in a make file. I have written a make file (named Makefile) but VS code insists on changing tabs to spaces so I get a "Missing Separator" error when I run make.
If I go to File > Preferences > Settings and type #id:editor.insertSpaces in the menu, I see this:
When I click on Modified elsewhere I see this:
The second screenshot seems to says that the editor won't insert spaces in a Makefile, but it certainly is. What am I doing wrong, or what have I failed to do?
Try modifing settings.json
VScode Settings
There are 3 levels (by higher priority)
Worspace Settings JSON
User Settings JSON
Default Settings JSON
This should fix most inconveniences:
"[markdown]": {
"files.trimTrailingWhitespace": false,
"editor.insertSpaces": false
},
What worked for me was adding the following to settings.json:
"[makefile]": {
"editor.insertSpaces": false
},
I am trying to add language based settings for several languages. In order to do so, I modified the file settings.json (see Configure language based settings in VS Code for more information) as follows:
{
"editor.tabSize": 4,
"editor.detectIndentation": false,
"editor.formatOnSave": true,
"vsicons.dontShowNewVersionMessage": false,
"[dart]": {
"editor.tabSize": 2
},
"[typescript]": {
"editor.tabSize": 4
},
"[c]": {
"editor.tabSize": 4
}
}
As you can see, I have the tab size option set to 2 for Dart and to 4 for other languages. The default value for indentation is 4 and "editor.detectIndentation" is disabled. But for some reason the editor doesn't get these settings:
For this reason, IndentRainbow extension does not work properly. On the bar below you can see that VS code still has "4" for tab size.
If I change the global setting for the tab size (editor.tabSize), VS Code just set the new value, e.g., 2 and I have then in all files and for all languages this value for indentation. I also tried to restart VS Code after settings were changed, it didn't help. What can I do to make what I want work properly?
settings.json is saved in C:\Users\MyName\AppData\Roaming\Code\User\
I found out for myself what the problem was. Under the extensions tab, I have the EditorConfig extension. This extension always overrides the user and workspace settings. I simply disabled this extension.
Another possible solution would be to create an .editorconfig file in the folder in which the project is located and to specify there the tab size.
I have applied the below settings in VS Code to get 4 spaces indentation.
But always when I open a new file, it switches back to 2 in the right-bottom corner.
If I click in the right-bottom corner and change the setting back to 4,
VSCode will still change back to 2 and still apply it with the 2-space auto-indent.
Alt+Shift+F
What am I missing?
Bit of an late answer. But just got the same issue solved...
Multiple things are able to control this. It also has taken me quite a bit of experimentation to get it corrected. For me point 3 below was the final trick to make it work. Before that, I noticed the editor loading with 4, but jumping back to 2 spaces. Now it stays at 4.
Some things to check:
1: VS Code configuration (Settings & Workspace, you can set these for system wide configuration or just for the current Workspace):
Check whether you have set:
"editor.tabSize": 4,
"editor.insertSpaces": true,
"editor.detectIndentation": false
And language specific settings (optional):
"[javascript]": {
"editor.tabSize": 4
},
"[typescript]": {
"editor.tabSize": 4
}
2: Are there any Extensions that could influence the indentation -> people have reported JS-CSS-HTML to also configure the setting.
3: Is there a .editorconfig file in your workspace? If so, check the settings over there. Angular creates one for example and configures the indent_size:
# Editor configuration, see http://editorconfig.org
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
max_line_length = off
trim_trailing_whitespace = false
Most answers focussed point 1 which, but for me the last step was important to make VS Code work properly.
This Stack Overflow handles all of the above in different answers:
Visual Studio Code: format is not using indent settings
I fixed it in the VisualStudio settings (1.31)
Go to: settings > workspace settings > Text editor
uncheck 'Detect Indentation' to stick to your default setting.
In many cases, it is Prettier that causes this. In fact, it just ignores all settings listed in #Oskar's answer.
So it needs to be overridden explicitly:
"prettier.tabWidth": 4,
"prettier.useTabs": true
And then just go to your file and hit Ctrlk,Ctrld, and the correct indentation should be applied.
See also Prettier is not indenting as specified.
Slightly different from previous answers. I had one file with the wrong indentation for its type and I wanted to correct only that file.
(If you must know: this Python script started out as text file with some queries in it. I got it from psql's -E \d echo look at the postgres schemas).
Anyway, this file was now a Python .py file, with a 2 spaces indentation. Not something that should be fixed by modifying general vscode settings.
What I did:
click on the bottom status bar spot that says 2 Spaces
choose Convert Indentation to Tabs on the dialog popup. Now it says Tab size 2
click on the status bar again where it says Tab size 2
choose Convert Indentation to Spaces. Now the dialog changes to propose indent size: 2 is selected. Pick 4 instead or any size you want.
Done
Basically there are different ways through this dialog, but if you get into tab mode and then switch back to space-based indentation, it will allow you to pick the number of spaces you want to use.
Be careful; this extension EditorConfig for VS Code interferes with vscode tab and indentation settings. Its installed by default but it is a nightmare. Disable it will solve all your problems.
Another problem I discovered with Python is that VS Code uses autopep8. No matter which setting I tweaked, VS Code seemed to ignore the 2 spaces setting. If you want 2 spaces instead of 4 - the fix is to add this to your settings.json.
"python.formatting.autopep8Args": [
"--indent-size=2",
"--ignore E121"
]
Btw you can see your autopep8 arguments by opening the command palette (⌘-shift-p on mac) and entering >Python: Show Language Server Output then switching to view the "Python" log.
This seems to be a common issue. See: VS Code Python autopep8 does not honor 2 spaces hanging indentation
Visual Studio Code Version 1.10 adds the ability to specify languages settings on a per language basis (*1). You put something like this in your settings.json file:
"[javascript]": {
"editor.fontSize": 100,
}
I'd like to do something more specific. I'd like to apply different rules to files that match *.min.js.
How would I do that?
I've actually got something that works, but it's a bit hacky, so I thought I'd ask.
*1) In case you want to know which ones: Use autocomplete after typing in "[]":, or see languageIds array in this file.
I'm aware beautify skips formatting min files by default. But just using "editor.formatOnSave": true, this doesn't seem to happen. Also, other non-formatting stuff like wordwrap is nice.
Here's my current solution:
"files.associations": {
"*.min.js": "javascriptreact"
},
// Hijack javascriptreact to create custom settings for min.js files
"[javascriptreact]": {
"editor.formatOnSave": false,
"editor.wordWrap": "on"
}
I'm using the fact that vscode happens to have a second type of javascript, one that isn't used my my project. Not ideal, but it seems to work.