I am having trouble finding a setting for new line spaces in VS code. Currently, I have it set up to a tab is the size of 8 spaces (after detecting indentation from content). This is because a principal software engineer edits in a different program, and set tabs to be 8 spaces, but code inside brackets were only indented 4 spaces.
Is there a way to set new lines following a bracket opening to tab only 4 spaces, but keep the rest of my tabs set to 8 spaces?
I've tried resetting tabs to only 4 spaces, but that changes the document as a whole and the indentation is off.
For example, when tabs are set to 8 (after detecting indentation from content), this is what it looks like (I added a comment to show where hitting a new line brings the cursor to):
if ((access(LOGFILE, F_OK)) != -1)
{
if(remove(LOGFILE) == -1) // Remove the older file
{
//this is where the newline starts
printf("Unable to delete the older log file.\n");
return ERROR;
}
}
Whereas, when I set tabs to 4, this is what it looks like (if statement indentation is bad):
if ((access(LOGFILE, F_OK)) != -1)
{
if(remove(LOGFILE) == -1) // Remove the older file
{
//this is where the newline starts
printf("Unable to delete the older log file.\n");
return ERROR;
}
}
Is there a way to change it so I can use the 8 tab format (first example), but when a new line is created after an if statement, it'll only add 4 spaces? Normally. I would just reformat the whole file with a formatter and select those options, but that would make looking at changes when pushing to git nearly impossible, plus the other software engineer still uses the tab 8, spaces 4 system to edit.
Any help is appreciated!
Indentation is same in whole file,
you cant use two saperate indentation at once
simple solution will be: set your tabs to 4 spaces and use tabs 1 and 2 times whenever needed.
For reference: Indentation | VS Code Docs
Related
I have file using an indentation level of 4 columns, and assuming that a tabulation character corresponds to 8 spaces, like this (I use . to represent a space, and <------> for a tabulation character):
class Foo {
....void bar() {
<------>if (boz) {
<------>....return x;
<------>}
....}
}
This is common for certain coding styles like Oracle coding conventions for Java:
Four spaces should be used as the unit of indentation. The exact construction of the indentation (spaces vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4).
I'm having trouble rendering this properly with VSCode:
If I set editor.tabSize to 4, then it renders badly as
class Foo {
....void bar() {
<-->if (boz) {
<-->....return x;
<-->}
....}
}
If I set editor.tabSize to 8, then the text is rendered properly, but the indentation guides are incorrect (a guide is missing for the void bar() indentation level):
More importantly, automatic indentation (pressing the "tab" key, or on-the-fly indentation when pressing "return" after a { character) now indents with 8 columns, making the editor barely usable.
An obvious workaround is to use only spaces for indenting, but this is not applicable when opening a pre-existing file.
Is there a way to configure the indentation guides to be displayed every 4 columns, while still rendering tabs every 8 columns?
In the editorconfig cross-editor configuration file specification, this corresponds to the tab_width and indent_size properties, that I would like to be able to change independently.
I'm a former Emacs-user, and this would correspond to tab-width and c-basic-offset for example.
The issue mentioned in the comments in 2020, microsoft/vscode issue 10339, has finally been closed in Nov. 2022(!)
PR 155450 enables having separate values for indentation and the display width of tab characters, which is a common requirement of some older projects and/or coding styles.
In addition to adding support for an editor.indentSize property, the indentation options on the status bar have been updated to allow independently configuring editor.indentSize and editor.tabSize.
So:
editor.indentSize: The number of spaces used for indentation or 'tabSize' to use the value from editor.tabSize.
This setting is overridden based on the file contents when editor.detectIndentation is on.
This should be available soon in VSCode Insiders and released with VSCode 1.74 (Nov. 2022).
Unfortunately, as of now there is no setting or extension that is based only on (spaces/tabs-at-the-current-tab-size).
This is the formatting I want to achieve:
function x() {
$elementOfSomething
.find(".class")
.remove();
}
This is the formatting VSCode does by default:
function x() {
$elementOfSomething
.find(".class")
.remove();
}
The default indentation is 4 spaces. In the second sample, the third and fourth lines are indented by 4 spaces in relation to the previous line. In the first sample, they are indented by 8 spaces. So I want to the default indentation to be 4 spaces but the continuation indentation to be 8 spaces. In NetBeans, there is the setting "continuation indentation" for this. Is there a way to achive such indenation behaviour in VSCode (for JS and Java)?
In VS Code this setting is called "wrapping indent", and this setting is located within the Text Editor category of settings.
Sounds like you want to set your wrapping indent to the "deepIndent" option, which will relatively indent the wrapped line to the width of two tabs. For example, if your tab size setting is 4 spaces, a deepIndent will be 8 spaces.
I have indented my files in my sublime text but when I push to github they don't look indented. How do I fix this?
The approach taken to indent file on sublime is:
select the code > Edit > Line > Reindent
looks like this on github:
Your issue looks to be caused by your use of literal tab characters for indenting as opposed to using spaces instead.
If there's a hotter holy war topic among developers than the debate of tabs versus spaces, it's probably related to how wide you should interpret a tab character to be for display purposes if you happen to use them.
In particular your images would appear to indicate that you think that tabs should be 2 characters wide and GitHub thinks they should be 8. As mentioned in this answer you can append an extra query field to the URL in GitHub in order to view the files the way you prefer them to be viewed.
As far as I'm aware that just changes how they're rendered on the page when you view and doesn't actually modify the file at all. If it's important that the file retain the same indent levels regardless of where or how you view the file, you should convert from tab indentation to space indentation instead since a space is unambiguously sized.
If you're using Sublime Text you can do that by clicking in the status bar where it says Tab Size: 2 and select Convert indentation to spaces; the status bar will switch to say Spaces: 2 to indicate that the indent has changed.
By this I mean if some file was written with 4 spaces, can you simply highlight it all and click on something to turn it into 2 spaces. I'm not sure if in practice (parsing) this would make sense/could lead to broken code.
I currently have my editor.tabSize set to 2, and sometimes I open files written with 4 spaces and I want to be able to turn them into 2 spaces. I have at least figured out to turn off the auto-detect so that when I highlight sections of the code and hit shift-tab, then tab again it will turn the selected code from 4 spaces into 2 spaces.
Is there a feature like this or does it make sense that this wouldn't exist?
To change the current document from using 4 spaces to 2 spaces:
Click on Spaces: 4 in the status bar or run the Indent using Spaces command
Select 2 for the new tab size
Run for Format Document command to apply the new indentation to the entire document
Having eclipse configured to use space instead of tabs, is it possible to configure eclipse so that the backspace key unindents like the tab key indents?
A demonstration to clarify what I mean with backspace unindent (the vertical bar stands for cursor position and the dots for spaces):
if(bar == 0) {
|foo = 0;
}
Pressing tab will indent 4 spaces:
if(bar == 0) {
....|foo = 0;
}
Pressing backspace only goes back 1 space:
if(bar == 0) {
...|foo = 0;
}
What I want is that it goes back 4 spaces:
if(bar == 0) {
|foo = 0;
}
You know that "shift+tab" will outdent a line already, correct?
Hmmm, I just took a look at preferences->general->keys. I only see indent line there, no outdent (or anything useful under "dent").
Personally, I would say that backspace is the key I hit second most often, after space. Rebinding it would drive me bonkers.
According to these very similar questions:
In Eclipse, how can spaces behave as tabs?
Is there a way to get Eclipse to treat 4 spaces exactly as it treats a tab?
it seems to be not possible :(
The feature will finally be available for next version of Eclipse 4.14 planned for 2019-12
https://www.eclipse.org/eclipse/news/4.14/platform.php#delete-spaces-as-tabs
Backspace/delete can treat spaces as tabs If you use the Insert spaces for tabs option, now you can also change the backspace and delete keys behavior to remove multiple spaces at once, as if they were a tab. The new setting is called Remove multiple spaces on backspace/delete and is found on the General > Editors > Text Editors preference page.
Hard way: set Eclipse to format indention using the tab character. Then backspace will remove the tab char
Easy way: start using the formatting feature of Eclipse to fix the formatting. Ctrl-Shift-F will format the selected lines or the whole file if there is nothing selected, using the formatting rules you configure.
Alternative: select the lines of text you want unindented and use shift-tab, it will unindent the selected lines.