Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move (up/down) code lines in Android Studio?

People also ask

How do I copy a line down in Android Studio?

Copy line: To copy a whole line place your cursor anywhere on that line and press Ctrl+C. This will automatically select the whole line and copy it to the clipboard.

How do I move up and down code?

You can use Ctrl + Shift + Up/Down Arrows to move the selected block up or down.

How do I arrange my Android code?

Navigate to the file in which we have to format our code and then click on the shortcut key as Ctrl+Shift+Alt+L after clicking this key you will get to see the below dialog on your screen. Select the formatting options and your code will be formatted and rearranged according to selected options.

How do you move a line up in VS?

ALT + UP/DOWN will move a line up or down. If you need this functionality in VS2012 (works with VS2010 too), take a look at the MoveLine Visual Studio Extension or the Productivity Power Tools suite.


To move a line:

Place the caret at the line to be moved.

Do one of the following:

On the main menu, choose Code | Move Line Up or Code | Move Line Down.

Press Shift+Alt+Up or Shift+Alt+Down.


If you want the exact behavior of eclipse you can do this:

File --> Settings --> Keymap --> Code--> Folding--> assign Alt+/ to "Move Line Up/down" instead of "Move Statement up/down"


There are (at least) two kinds of Moving line up/down in Android Studio: The "intelligent" one and the "dumb" one. Like IngoAlbers said, the dumb one (Shift+Alt+<Arrow>) just moves the line.

Using Ctrl+Shift+<Arrow> instead, makes the functionality more intelligent:

  • It doesn't leave the current "context":

    public static void test() {
        int i = 5;
        Runnable theodor = new Runnable() {
            public void run() {
                System.out.println("Hi!");
            }
        };
    }
    

    Moving the int i = 5;line down one step, brings you this:

    public static void test() {
        Runnable theodor = new Runnable() {
            public void run() {
                System.out.println("Hi!");
            }
        };
        int i = 5;
    }
    
  • It keeps methods together:

    public static void hello() {
        System.out.println("Hello!");
    }
    
    public static void dinner() {
        System.out.println("Dinner's ready!");
    }
    
    public static void sleep() {
        System.out.println("Good night.");
    }
    

    Moving the line public static void sleep() { up one step moves the complete sleep() method above dinner():

    public static void hello() {
        System.out.println("Hello!");
    }
    
    public static void sleep() {
        System.out.println("Good night.");
    }
    
    public static void dinner() {
        System.out.println("Dinner's ready!");
    }
    
  • In most cases, it is just annoying. ;-)


In windows, you can go to 'Keymap' settings(File --> Settings --> Keymap --> Editor Actions) to customize this action and more actions.

I think it's a better way.