Reformat Your Code

Reformat a specific piece of code or the whole file according to your preferences.

The Problem

You're busy hammering out line after line of amazing code but when you're done and you look up to marvel at your masterpiece, you see that the indentation is all wrong.

The Solution

You can quickly reformat the whole file by invoking "Reformat Code" ⌘⌥L (macOS) / Ctrl+Alt+L (Windows/Linux).

reformat-file-java.png

As you'd expect, this works in any JetBrains IDE.

reformat-file-python.png

However, what if you just want to reformat just one block of code? For example, maybe the whole team changed their code style, and you don't want a large full file reformat to be part of this specific commit. In that case, select the code you want to reformat and again invoke "Reformat Code" ⌘⌥L (macOS) / Ctrl+Alt+L (Windows/Linux).

reformat-block-c-sharp.png

You can reformat code at the directory level too! Invoke your Project tool window ⌘1 (macOS) / Alt+1 (Windows/Linux), select the directory you want to reformat and invoke "Reformat Code" ⌘⌥L (macOS) / Ctrl+Alt+L (Windows/Linux) again. A word of warning here, you probably don't want to mix large commits that include reformatting and code changes - it can make deciphering the commit harder for the next person.

Lastly, there are additional settings you can use for reformatting your code which you can access by invoking the "Reformat Code Dialog" ⌘⌥⇧L (macOS) / Ctrl+Alt+Shift+L (Windows/Linux). The options here will vary depending on which type of code you're working with so try it out and see what you can learn!

reformat-options.png


Related Resources

Find in Path with the Navigation Bar
Find in Path with the Navigation Bar
Use the Navigation bar and Find in File together and be faster than all of your team members.
Speed search with the Navigation Bar
Speed search with the Navigation Bar
Use the keyboard abbreviations to quickly find what you're looking for in the Navigation bar.
Create a new File from the Navigation Bar
Create a new File from the Navigation Bar
A fast way to add a new file to your project, without touching the mouse.