34

Is there a way/shortcut/built-in feature that can align code for operands, like '=' signs?

For example, there is a XAlign for Xcode (https://github.com/qfish/XAlign), allowing the user to select code that needs to be aligned and use a shortcut to align it automatically.

There is a Fields Group feature in Android Studio which can "Align in columns", but it does not work for code that is already written.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Daksharma
  • 599
  • 1
  • 7
  • 9
  • 2
    possible duplicate of [Code formatting shortcut in Android studio](http://stackoverflow.com/questions/16580171/code-formatting-shortcut-in-android-studio) – Jared Burrows Jan 29 '15 at 19:45
  • I don't think there's a built in formatter for aligning by `=` in IntelliJ. – nmr Jan 29 '15 at 20:18

6 Answers6

55

Indent code in Android Studio:


Windows

Ctrl + Alt + L


Mac:

Option + Command + L

Community
  • 1
  • 1
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 1
    That changes the screen orientation on my laptop. VERY annoying. – Eric Engel Dec 25 '16 at 02:16
  • @CoolMind, Thanks for the suggestion, but I no longer have that laptop. I am on a MacBook now. – Eric Engel Apr 17 '17 at 03:31
  • Works good on a file level but I wonder if there is a way to do modification on entire project or even better to do it automatically when I open a project? Upvoted – pixel Sep 10 '17 at 19:49
  • @pixel Use the re-format code shortcut in the project/android file view highlighting/selecting the folder thereby navigation to Code > Reformat – Binod May 24 '20 at 13:41
  • 1
    Working on macbook Catalina. Thanks – serif Dec 05 '22 at 13:09
6

In Eclipse which comes with Android Studio Bundle, the shortcut key is as follows to initiate Auto Formatter:

Ctrl + alt + L

If you want to change this combination, go to menu Preferences → Keymap and set to custom.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • I am on Mac. [Command + Option/Alt + L] does not work. i have the Field group "Align in column" turned on. but selecting code and reformatting does not seem to work. – Daksharma Jan 29 '15 at 20:10
  • Yes. Keymap are default. using the shortcut bring up the Reformat Code but it does not reformat the code. the = sign does not align for variable declarations for the selected code. – Daksharma Jan 29 '15 at 20:17
  • i think it's CTRL + SHIFT + F – Neo Feb 01 '16 at 11:29
6

The alignment for variable declarations works for only those that are outside of functions. The alignment feature in menu Preference → Code Style → Wrapping and Braces → Fields Groups → "Align in Columns" does not work for those inside the brackets/braces.

Here is an issue ticket on the JetBrains website: https://youtrack.jetbrains.com/issue/IDEA-56947#

This feature is available, but not in the stable release yet. It is available from build: 140.1761

At the time of writing this answer my current build is "135.1653844, built on December 18, 2014" on Stable Release.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daksharma
  • 599
  • 1
  • 7
  • 9
3

Windows:

Ctrl+Alt+L

Linux:

Ctrl+Shift+Alt+L

macOS:

⌥ option+⌘ command+L

slava
  • 791
  • 1
  • 11
  • 26
Sudhir Singh
  • 817
  • 11
  • 16
1

For Windows and Linux in Android Studio:

Ctrl + Alt + L

For Mac:

Option + Command + L

Ramesh R
  • 7,009
  • 4
  • 25
  • 38
0

Ubuntu Linux: Ctrl-alt-L (suggested by other responses) is captured by the window manager and locks the screen. So you need to change it: file->settings->keymap->main menu->code->Reformat Code. I use a double tab since I don't want the tab to shift a selection right.

This formats the code as you've specified in your preferences. To handle your specific request (aligning equals statements) go to: (file->settings->editor->code style->(choose language)->Wrapping and braces->Group declarations->align variables in columns

These styles are language specific so this particular option might not exist in the language you chose, or may be named differently.

AndrewStone
  • 517
  • 4
  • 14