What's the equivalent for eclipse's ALT+UP/DOWN (move line) in Visual Studio?
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.
Comments
-
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 over 11 yearsAlso 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 thenamespace
declaration. When the line containing theusing
is on the line below thenamespace
declaration, R# will combine them and the next call to <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>Alt</kbd> + <kbd>↓</kbd> will move both thenamespace
and theusing
line all the way to the end of the formernamespace
code block. -
void.pointer over 9 yearsUnbelievable that you need a plugin for this
-
Robin Coe over 8 yearsAlso doesn't seem to work to move lines across method boundaries.
-
Arash about 8 yearshow 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 over 7 yearsIf you want to rebind it, the name of the command is "Edit.MoveSelectedLinesUp", under Tools > Options > Environment > Keyboard.
-
Sentry over 7 yearsNot quite the same, it doesn't correct the indentation
-
James Freitas about 7 yearsIn Ubuntu 16.04 worked out of the box! Thanks a lot!
-
Shimmy Weitzhandler almost 7 yearsHi, 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 over 6 years@Sentry ctrl+k,D
-
scottheckel about 5 years@Shimmy you actually have a better option with Ctrl + Shift + Alt + Direction keys. You can move it anywhere.
-
Andrew W almost 4 yearsI also found that these had no default key-binding in VS 2019
-
IneffaWolf about 3 yearsAlt+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 almost 3 years@IneffaWolf, anyway, bindings
Format.SpaceDown
andFormat.SpaceUp
can be executable within VC Dialog Editor (at least in VS2019 it is so). If you assign the same key bindings toEdit.MoveSelectedLinesUp/Down
but only for C# Editor, for example, I believe there won't be any conflict.