I'm really liking VS Code, but I've looked everywhere and I can't seem to change one very annoying feature: pressing return on an empty indented line carries over the current indentation, but removes all indentation on the original line. It seems impossible to have two consecutive tab-indented lines in the editor.
Is there a setting I'm missing or some other way to preserve the tab-indentation across lines? Thanks.
We can setup user settings for this,
Go to File > Preferences > User Settings:
You can customize this easily via these 3 settings in
/ The number of spaces a tab is equal to. This setting is overriden
// based on the file contents when editor.detectIndentation is true.
"editor.tabSize": 4,
// Insert spaces when pressing Tab. This setting is overriden
// based on the file contents when editor.detectIndentation is true.
"editor.insertSpaces": true,
// When opening a file, editor.tabSize and editor.insertSpaces
// will be detected based on the file contents.
"editor.detectIndentation": true
For more information -
We can setup user settings for this,
Go to File > Preferences > User Settings:
You can customize this easily via these 3 settings in
/ The number of spaces a tab is equal to. This setting is overriden
// based on the file contents when editor.detectIndentation is true.
"editor.tabSize": 4,
// Insert spaces when pressing Tab. This setting is overriden
// based on the file contents when editor.detectIndentation is true.
"editor.insertSpaces": true,
// When opening a file, editor.tabSize and editor.insertSpaces
// will be detected based on the file contents.
"editor.detectIndentation": true
For more info you can check here - How to set tab-space style?
I have figured out my problem:
In Settings, "editor.trimAutoWhitespace" is true by default, meaning any trailing tabs left on a line are automatically deleted, even if they are just holding the regular indentation level. I have fixed my problem by setting this option to false in my User Settings (File / Preferences / Settings).
I am surprised that I can't find any record of anyone else having found this behavior unusual.
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
},
My indentation are too thin. But I also don't want to use more than 2 spaces for indentation. Is there a way to increase the size?
Current view,
Expected View,
Since you aren't using any formatter type extension
You can set your indentation easily by selecting the indenting option in the status bar.
and then select the size you want.
If you want to globally have the same settings.
add these values to your settings.json
"editor.tabSize": 4, //4 = 4 spaces
"editor.detectIndentation": true, // this will auto detect whether your file is using spaces or tabs to indent.
"editor.insertSpaces": true, // Insert spaces when pressing Tab. This setting is overridden based on the file contents when #editor.detectIndentation# is on.
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?
Add this line to your settings.json file.
"[markdown]": {
"files.trimTrailingWhitespace": false
}
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
}
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.
(Theme Light Pink)
This example above is for line wrapping, but you can adapt it to reference trim_trailing_whitespace.
Do Ctrl-K s. This will "Save without formatting", which also means, without trimming trailing whitespace in the file you're editing
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
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