What's the equivalent for eclipse's ALT+UP/DOWN (move line) in Visual Studio?

66,710

Solution 1

In Visual Studio 2013 and later, this functionality is built in. 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.

Solution 2

ReSharper's Ctrl + Shift + Alt + /// is even more powerful - when on the beginning of the line, it will move the entire line, but can also be used to move entire methods, change the order of parameters, etc.

Solution 3

For me in Visual Studio 2019 it comes default closed.

For open it:

Tools -> Options -> Keyboard then select Edit.MoveSelectedLinesUp, click "Press shortcut keys" input and press Alt + Up (or whatever you want for it). And the other one is Edit.MoveSelectedLinesDown, click "Press shortcut keys" input and press Alt + Down (or whatever you want for it).

Solution 4

This is now working out of the box with Visual Studio 2013, same way as in Eclipse.

Solution 5

In Visual Studio 2013 and later, you can move lines up and down using Alt + / Alt + . Unfortunately this will not adjust the indentation if you move between blocks, as of today only ReSharper (and all other IDE's by Jetbrains including Rider) can help you with that.

To move entire blocks of code around you can move the cursor to its head and press Ctrl + m twice fast to collapse it, and then use the command above to move it around.

Here is a neat reference to all default keyboard shortcuts in different versions of Visual Studio.

Share:
66,710
Roberto
Author by

Roberto

Computer Engineer, MEng.

Updated on November 04, 2020

Comments

  • Roberto
    Roberto over 3 years

    In Eclipse, selecting a line and pressing Alt + / will move the line up and down, a quick way to avoid copy&paste. Is there an equivalent in Visual Studio?

  • Carl G
    Carl G over 11 years
    Also doesn't work if R# thinks the current line is part of a larger construct. E.g., try moving a using statement from the top of a file a few lines down to be inside the namespace declaration. When the line containing the using is on the line below the namespace declaration, R# will combine them and the next call to <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>&darr;</kbd> will move both the namespace and the using line all the way to the end of the former namespace code block.
  • void.pointer
    void.pointer over 9 years
    Unbelievable that you need a plugin for this
  • Robin Coe
    Robin Coe over 8 years
    Also doesn't seem to work to move lines across method boundaries.
  • Arash
    Arash about 8 years
    how about child element when you select a tag ? for example I have selected a div tag and I want to move up all child element , instead of selecting by mouse .
  • Joan Charmant
    Joan Charmant over 7 years
    If you want to rebind it, the name of the command is "Edit.MoveSelectedLinesUp", under Tools > Options > Environment > Keyboard.
  • Sentry
    Sentry over 7 years
    Not quite the same, it doesn't correct the indentation
  • James Freitas
    James Freitas about 7 years
    In Ubuntu 16.04 worked out of the box! Thanks a lot!
  • Shimmy Weitzhandler
    Shimmy Weitzhandler almost 7 years
    Hi, I installed ReSharper which screwed up some of my VS shortcuts, including this one. What's the name of the command or the way to reset this in VS2017?
  • John Demetriou
    John Demetriou over 6 years
    @Sentry ctrl+k,D
  • scottheckel
    scottheckel about 5 years
    @Shimmy you actually have a better option with Ctrl + Shift + Alt + Direction keys. You can move it anywhere.
  • Andrew W
    Andrew W almost 4 years
    I also found that these had no default key-binding in VS 2019
  • IneffaWolf
    IneffaWolf about 3 years
    Alt+Up and Alt+Down are already bound to Format.SpaceDown, though I don't even know what that does. You think I can rebind it?
  • Barabas
    Barabas almost 3 years
    @IneffaWolf, anyway, bindings Format.SpaceDown and Format.SpaceUp can be executable within VC Dialog Editor (at least in VS2019 it is so). If you assign the same key bindings to Edit.MoveSelectedLinesUp/Down but only for C# Editor, for example, I believe there won't be any conflict.