ChatGPT解决这个技术问题 Extra ChatGPT

How can I use tabs for indentation in IntelliJ IDEA?

How can I use tabs instead of multiple spaces for indentation in IntelliJ IDEA 11.0?

I have "Use tab character" checked under "Code Style" > "General" > "Default Indent Options". And also tried to have "Smart tabs" checked, but it doesn't help.

From the documentation:

If this check box is selected, tab characters are used: On pressing the Tab key For indentation For code reformatting Otherwise, spaces are used instead of tabs.


g
grg

File > Settings > Editor > Code Style > Java > Tabs and Indents > Use tab character

Substitute weapon of choice for Java as required.


Note that if you're changing this and also replacing all the tabs/spaces in a file, you may need to close and reopen the file to get the detection corrected after your changes.
Any idea why "Use tab character" is not enabled by default?
@IvanBalashov Tab characters can cause some formatting problems when viewed in other text editors. In addition, some languages, like Python, can break when you use tabs instead of spaces.
and what do I do when I want to have Tabs as indentation FOR ALL LANGUAGES?? should I really click through 500 different languages and apply it individually?
I've done this and IntelliJ is still using spaces (see post why-wont-intellij-use-tabs )
R
ROMANIA_engineer

IntelliJ IDEA 15

Only for the current file

You have the following options:

Ctrl + Shift + A > write "tabs" > double click on "To Tabs" If you want to convert tabs to spaces, you can write "spaces", then choose "To Spaces". Edit > Convert Indents > To Tabs To convert tabs to spaces, you can chose "To Spaces" from the same place.

For all files

The paths in the other answers were changed a little:

File > Settings... > Editor > Code Style > Java > Tabs and Indents > Use tab character

File > Other Settings > Default Settings... > Editor > Code Style > Java > Tabs and Indents > Use tab character

File > Settings... > Editor > Code Style > Detect and use existing file indents for editing

File > Other Settings > Default Settings... > Editor > Code Style > Detect and use existing file indents for editing

It seems that it doesn't matter if you check/uncheck the box from Settings... or from Other Settings > Default Settings..., because the change from one window will be available in the other window.

The changes above will be applied for the new files, but if you want to change spaces to tabs in an existing file, then you should format the file by pressing Ctrl + Alt + L.


That is what I call an explanation! Thanks a lot. I am used to spaces and I had set the other things as well, but I had missed the reformatting.
R
Rüdiger Herrmann

I have started using IntelliJ IDEA Community Edition version 12.1.3 and I found the setting in the following place: -

File > Other Settings > Default Settings > {choose from Code Style dropdown}

Wouldn't have found this without your answer. Thanks!
You may need to unselect "Detect file indentation"
v
visheshd

For anyone not able to get this, another thing you need to uncheck the following as well

Preferences > Editor > Code Style
[] Enable EditorConfig support
EditorConfig may override the IDE code style settings

My IntelliJ version 15.0.4


Thank you! You just saved my keyboard.
I was having 2 spaces as tab size and was trying since last many days to fix this. This is how I fixed the issue. Thanks a lot @visheshd
D
Dmitri Algazin

Another useful option in IDEA to switch off or keep checked if you really need that:

Preferences -> Code Style -> Detect and use existing file indents for editing

if your team is going to switch to tab formatting with existing code written with spaces, uncheck that


v
vepanimas

Have you tried .editorconfig? You can create this file in the root of your project and configure indentation for different file types. Your code will be automatically formatted. Here's the example:

# top-most EditorConfig file
root = true

# matches all files
[*]
indent_style = tab
indent_size = 4

# only json
[*.json]
indent_style = space
indent_size = 2

This file overrides anything else in the intellij settings. It is very important to check this file first.
Having tried everything--accepted or otherwise--on this page, this is the only thing that overcame the heavy hand of spaces (Macos, Intellij CE 2018.3.4, existing .rs file).
H
Haimei

My Intellij version is 13.4.1

Intellij IDEA->Perference->Code Style(Project Setting)

G
GrayedFox

To expand on @Dmitiri Algazin 's answer: settings for individual languages are overridden by the general setting

Preferences -> Code Style -> Detect and use existing file indents for editing

So if you are wondering why your new settings are being ignored after changing your settings for a specific language, there is a chance this checkbox is ticked.

As a side note; changing any default settings automamagically creates a settings profile clone (i.e. Default(1)) which I assume is in place so that the default IDE settings are never overwritten.

It is a little confusing at first, really, whether editing Default settings or Project Settings is going to have any effect on your project, since you can select Default from the drop down menu and then edit from there.

If you don't want to keep seeing random clones of Default populating your settings profiles, edit the Project Settings directly.


M
Mafick

For those who are having trouble indenting phpstorm here I have a tip and I hope they help ...

https://i.stack.imgur.com/iAuP0.png

If they are on the windows machine. If you are on mac and choose macos.