Search code examples
android-studiocode-formatting

How to remove tabs on multiple rows at once


This is not a programming question but an inconvenience in the android-studio editor.

If you have an unwanted tab before all your lines, how can you remove them all at once? Now I have to manually go over 50 lines to remove all the tabs to make my code look clean.

If you want to add multiple tabs at once you just select all your code and press the tab button. So I'm looking for the reverse of that.


Solution

  • If I understood you right, you want to beautify the code itself. Fortunately, you don't have to do that manually - at all.

    There's a keybinding for it, which may vary by your OS and which layout you use by default. Go to file -> settings -> Keymap and search for auto-indent. Here's what I get on Windows 10 with the default keymap:

    enter image description here

    Again, which you have may depend on OS (I'm assuming that mainly applies to Mac though) and your keymap, but you can automatically indent your code as per language standards using Ctrl+Alt+I.

    Note that this mainly does indentation. If you chose to golf code and want to ungolf it, this will not work. At least it doesn't for Java.

    However: This only works with code files the IDE or plugins support. This won't work for i.e. a .txt file out of the box.


    If I misunderstood you and you only want to remove tabs without doing the auto-indent, there's at least two other options.

    The first option is using multiple cursors. You can add an additional cursor with shift+alt+a mouse click where you want the cursor, or holding the mouse wheel and moving the cursor with the mouse wheel held down. There might be other methods as well, but those are the two I know of.

    Once you have multiple cursors, delete the tabs just like normal. But be careful! Doing so might delete the entire line itself. If it does, you can do 1 tab/n units per indent level to the left, and press delete instead.

    There's (AFAIK) no limit to how many cursors you can have at once, but you can theoretically do it with 50 lines at once if you want to. But general advice - don't add more cursors than you can see at once. These do run in parallel and it's easy to lose track if you're not careful, and you might end up deleting stuff you didn't want to delete.


    And finally, the regex solution:

    Note: Be careful with this. If you use it incorrectly, you might get unwanted results.

    If you only want to do this in a limited area, highlight it first. Then CTRL+R and you'll be presented with the regular replace menu. Make sure Regex and In Selection are selected.

    A base regex to go off is ^([\s]{2,4}|\t). Explanation just for reference:

    ^ - At the start of the line
    (
        \s{4}   - Match 4 spaces
        |\t     - Or a tab character
    )
    

    Replace with nothing and click "replace all" (or just use the regular "replace" button if you want to double-check before you do anything). This will replace one occurrence of 4 spaces or a single tab character. If you use indentation that isn't based on 4, change the number.

    This is only useable and useful if you've found yourself with incorrect indentation that's the same across all the relevant lines - it will not fix indentation mistakes and/or inconsistencies such as 3-space indentation when you want 4, or random indetation for the same block. Use the first or alternatively second method for that instead.