6

I know how to convert indent for a single file. I go to edit -> convert indent -> space/tab.

But I want to do this for all files under a directory.

I try click on a directory and then go to edit -> convert indent, but the options are grayed out.

enter image description here

user2628641
  • 2,035
  • 4
  • 29
  • 45

2 Answers2

8

You can use the shortcut Ctrl+ALT+L (Windows/Linux) or ⌥⌘+L (MAC OS X) and select the Rearrange entries option to reformat the code in the current file or reformat a module or directory.

You can also Right-click a module, file, or directory from the context menu and select Reformat Code and also select the Rearrange entries option.

This will convert the indents for all files/directories selected:

enter image description here

This works on most of the Jetbrains IDES (iDea, PyCharm, WebStorm, RubyMine, and so on.)

valdeci
  • 13,962
  • 6
  • 55
  • 80
  • 2
    Reformatting/Rearranging code is not what the OP asked for. Changing the indents is just a minor fraction of what *Reformat code* does and is irrelevant to *Rearrange entries*. I'm also looking for something like this, in order just to change indents in a badly formatted codebase, without breaking the git history tracking completely. – Giovanni Jun 02 '21 at 13:24
0

It seems there is no such dedicated option in IntelliJ, but you could just work around it using a "low-level" Replace All action.

  • Open the Edit → Find → Replace in Files... dialog
  • In case you want to convert spaces to tabs, you should
    • Enter in the Find field (i.e. four spaces (or whatever number of spaces the project is currently indented with))
    • Press the Regex search modifier (Alt + X)
    • Enter \t in the Replace field
    • NB: In case you have valid strings with 4+ spaces in them, they will get replaced too. In most use cases, however, this is not happening.
  • In case you want to convert tabs to spaces, you should do the same as above, but swap the Find and Replace field contents
    • NB: Again, if you have valid strings with tabs in them, they will get replaced too. I haven't had this use case, because I've only needed to convert in the opposite direction.
  • You will probably also want to set a File mask in order not to replace spaces in code-irrelevant files
Giovanni
  • 332
  • 3
  • 12