Reputation: 4177
I know I can change indentation by:
File > Settings > Editor > Code Style > ANY_LANGUAGE > Tabs and Indents > Use tab character
but in my legacy project there is a mix of indents in different files. Is there any keyboard shortcut in IDE for changing spaces to tabs and vice versa? Or how can I set up one for all or specified language files?
Upvotes: 45
Views: 84207
Reputation: 4414
In IntelliJ IDEA, to convert existing tabs to spaces or vice versa, use File → Edit → Convert Indents → To Spaces (or To Tabs). The Reformat code shortcut Ctrl+ Alt+ L will also do this, while applying other changes.
To tell the IDE to insert spaces for tabs going forward, go to File → Settings → Editor → Code Style → (whatever language) → Tabs and Indents → then uncheck Use tab character.
There's another setting that you may also want to pay attention to that controls file indents: File → Settings → Detect and use existing file indents for editing. This setting is not always consistent for some reason. If you uncheck it, then IntelliJ may or may not auto-indent new lines in your file with tab characters.
For example, let's say each line in one of your java file already begins with a tab character. You want to leave those, say for source control reasons, but you want all new code to substitute spaces for tabs, including indents. So you have Detect and use existing file indents for editing unchecked and you have Use tab character unchecked for Java. You're clearly telling IntelliJ not to insert a tab, even for indents.
Well, nonetheless, when you hit Enter at the end of a new line that begins with a tab, IntelliJ goes ahead and adds a tab character at the beginning of the next line you just created. Hit tab again and it adds spaces (as expected based on your settings).
If someone knows how to resolve this somewhat irritating unexpected behavior, please comment. Granted, it's pretty easily resolved by first doing Ctrl+ Alt+ L to change all tabs in the file to spaces and then going forward you won't see this strangeness. But of course that modifies source history for any changed lines.
Upvotes: 13
Reputation: 539
Editor > Code Style > Detect and use existing file indents for editing
This will make "tab" into four spaces, if the file is indented with spaces and also lets you show a notification when a file is indented with tabs, and offer to change it to spaces for you.
Note: I am on the pro version on Intellij, if that makes a difference.
Upvotes: 1
Reputation: 3234
Select a folder, do :
Cmd+Shift+R
in MacOs for eg. [\t]
as text to be replaced as the Convert Indents action does it file per file, which is not very convenient with big project.
Note: After you still need to change default preference to use spaces as indentation like explained by @DimaSan
Upvotes: 11
Reputation: 29
You can reformat each file to apply your styling in Code > Reformat code menu. On Windows, the related shortcut is Ctrl-Alt-L.
Selecting a package will give you an option to reformat all source files including sub-directories, optionally filtered by file extension.
This is as of IntelliJ IDEA 2018.2.3 for Java files.
Upvotes: 0
Reputation: 12674
To toggle between tabs and spaces:
On the main menu, choose Edit → Convert Indents, and then choose To Spaces or To Tabs respectively. Read more about Changing Indentation.
You can quickly do the same with Navigating to Action menu by pressing Ctrl+Shift+A and type convert indents
in the field:
If you want to set spaces indents by default, go to Settings → Editor → Code Style → Java, make sure the Use tab character checkbox is unchecked and set the Indent value:
Upvotes: 70