81

In VS2010 I had a shortcut set up Alt+Up/Down to move lines of code up or down, just like I used to have in Eclipse in my Java days.

Now since Power Tools and macros (which is how I set up this shortcut in VS2010) are not available in VS2012, how do I get this to work?

I've seen somebody mentioned that Resharper can do this, but for the love of fire and water, I can't find the command names in the list of commands that can do that. I have tried ReSharper_MoveUp/MoveDown and that does not move lines up/down:

Move_Up command did not do the trick

This moves blocks of text up and down. I just want line-by-line up or down, no fancy logic there. Before

What I have

After ALT+Up (when cursor is on WriteLine line) I would like to see this:

Line moved one up, inside the bracket, indented

I have seen these threads: What's the equivalent for eclipse's ALT+UP/DOWN (move line) in Visual Studio?, Visual Studio: hotkeys to move line up/down and move through recent changes, but none of the suggested solutions work in VS2012.

Any other ideas?

UPD 7 Nov 2012: . Extensions are catching up with Visual Studio 2012. Now I know at least 3 extensions that provide this functionality:

UPD 15 Nov 2012: Just stumbled across another extension that claims it does what is required here. Have not tried it though: LineMan Extension

UPD 10 May 2015 Just installed VS2015 RC and this is built-in already: MoveLineUp/Down

2
  • 2
    FYI PowerTools 2012 is out and has this now.
    – Carl G
    Nov 21, 2012 at 12:58
  • This also applies for atmel Studio 7 for moving lines up and down.
    – Cutton Eye
    Dec 28, 2020 at 2:28

9 Answers 9

65

Since VS 2013, the shortcut comes with built-in keybinds:

  • AltUp
  • AltDown

These move the selected line(s) up and down. If a line isn't selected, but the text entry cursor is on the line, that also works without having to select the line.

If you wish to rebind it in Tools > Options > Environment > Keyboard, the identifier keys are Edit.MoveSelectedLinesUp and Edit.MoveSelectedLinesDown. You may need to remove your new combination from other existing keys first.

8
  • The question was about 2012.
    – trailmax
    Jul 30, 2014 at 23:17
  • 23
    @trailmax I'm aware, and that already got its answers but I'm posting this so that people using 2013 can be aware that this already exists so they don't go searching farther like I had to.
    – Keavon
    Jul 30, 2014 at 23:22
  • @RayL Maybe you rebound those keys to something else?
    – Keavon
    Aug 5, 2014 at 21:46
  • 4
    @RayL Edit.MoveSelectedLinesUp and Edit.MoveSelectedLinesDown. Please report back if those work for you.
    – Keavon
    Aug 6, 2014 at 17:55
  • 1
    @keavon, That works but For some reason I'm not able to assign "Atl + up arrow" to that command....maybe has something to do with resharper or powertools being installed. I think that's what initially screwed me up. Odd. "ctrl+alt+UP Arrow" works great though! Thanks. Aug 6, 2014 at 19:59
35

Update: huzzah, Productivity PowerTools VS2012 is now available and offers this functionality (along with another personal favorite, Tools.AddEndTokenAtTheEnd.)

MoveLine Extension (compatible with VS 2012; I had to manually restart my VS in order to see the commands in VS's keyboard shortcut tool.)

3
  • 3
    When you setup a keyboard shortcut, make a scope to "Text Editor".
    – Youngjae
    Oct 10, 2012 at 0:32
  • They key to success here is that your shortcut should scope to Text Editor AND you need to REMOVE (REMOVE) existing keyboard shortcuts already assigned. If you have resharper you need to remove 'Resharper.Resharper.GoToNextMethod' and '..GoToPrevMethod' (note there may be two shortcuts (XAML and Text Editor) that need to be removed.
    – kingdango
    Sep 3, 2013 at 14:15
  • @Carl G - Is there any way to move horizontally word by short cut? like Alt + Up or Alt + Down ?
    – Sasan
    Apr 17, 2017 at 6:09
10

Install ReSharper.

Open Tools -> Options -> Environment -> Keyboard (as shown in a picture)

  1. Find "ReSharper.ReSharper_MoveUp"
  2. In "Use new shortcut in:" choose: "Text Editor"
  3. Click in "Press shortcut keys:" and press your shortcut (for example Alt+ArrowUp)
  4. Press "Assign" button Same with with "ReSharper.ReSharper_MoveDown"

P.S. Works fine for single lines and multiple selected lines, but removes selection after moved 1 line. Hope there are way to move multiple lines multiple times in one selection.

3
  • 2
    nah, resharper is trying to be too helpful and pushes it too much this time. It moves the block of code with current line. And all I want is to simply move one line up or down, not a related block.
    – trailmax
    Aug 17, 2012 at 22:11
  • For me "ReSharper.ReSharper_MoveUp" and "ReSharper.ReSharper_MoveDown" commands moves only one current line if no selection, even inside block. Not sure why you have it another way. I use ReSharper 7.0.97.60 and VS 2012 RTM from MSDN. Aug 18, 2012 at 14:34
  • 2
    An example of where this does not work is when a using statement appears at the top of a file, and the user wants to move the statement down several lines so as to be within the namespace definition. Using R#'s MoveDown command works correctly for a few iterations, until the using statement appears just under the namespace line. Now the next iteration will attempt to move the namespace below its current block.
    – Carl G
    Oct 2, 2012 at 18:20
7

If you have Resharper, then you can hit Ctrl + Shift + Alt + to move the current line up. or Ctrl + Shift + Alt + to move one line down.

This works in VS 2012 Premium with Visual Studio keyboard scheme in Resharper 7 (7.0.97.60 to be precise).

Edit: This is mapped to 'ReSharper.ReSharper_MoveUp'/'MoveDown' commands.

4
  • 1
    That does not work for me for some reason. I've got exactly the same resharper and map the same commands. And whenever I try to move the lines up/down, they are jumping in a crazy way: sometimes up into another method, sometimes down below next block. But not what I want -(
    – trailmax
    Aug 20, 2012 at 23:41
  • Yups - I saw that, albeit a bit too late. Did you tried resetting your keyboard mapping? Might be the shortcuts are mapped to a different command? Your screen shot says it's mapped for XAML, can you see if it's mapped for Text as well? Aug 21, 2012 at 2:39
  • Yep, did check for Text Editor. In fact, I have just reinstalled VS2012 and Resharper and it has default mapping. Your shortcuts do work, but it does not move one line up or down - moves a block up or down -(
    – trailmax
    Aug 21, 2012 at 15:08
  • It moves the complete code block if the cursor is placed on line beginning / ending code block. Did you tried selecting the entire line and then trying the shortcut? Aug 21, 2012 at 16:18
6

In Visual Studio 2017(atleast) I have below

enter image description here

1
  • Its in VS2019 where you show, but the keys were not mapped (at least in my installation). Had to map the keys manually.
    – ΩmegaMan
    Feb 12, 2020 at 21:05
2

You can use Move Line Command extension.

Just follow formula349's comment:

I was able to get this working with 2012 as-is.

Rename the package to .zip and unzip all files into a folder. Edit the extension.vsixmanifest file and look for the SupportedProducts XML node. Change the Version="10.0" to Version="11.0".

Re-zip the folder contents and rename back to .vsix. Works great!

2
2

While a lot of people seem to be recommending misc. Visual Studio Extensions and Resharper, I would like to point out there is a native hotkey that accomplishes this goal. I came to this question trying to find the answer, as my Resharper replaced my default hotkeys with their MoveUp and MoveDown versions.

And while Resharper's hotkeys do move things up and down, they are not what the original poster are looking for, and they are not what I wanted. These hotkeys move methods and blocks of code, not individual lines.

Visual studio has two hotkey commands to move your selected lines upwards or downwards. These hotkeys are:

  • Edit.MoveSelectedLinesDown
  • Edit.MoveSelectedLinesUp

I am using Visual Studio 2013 however, so I am unaware if these exist in an older version. If 2012 does not have these hotkeys then perhaps an extension is the only solution.

Either way, I had an issue locating the exact hotkey within visual studio's list and it took me a good while to find these, so I hope that my solution manages to help someone else in a similar situation to my own.

2
  • Aye, these did not exist in VS2012. And second answer states that VS2013 has these.
    – trailmax
    Dec 12, 2014 at 0:16
  • Preach! Resharper is frowned upon in my current company, so I'm trying to get around it... after uninstally Resharper all my shortcuts are busted :( Jun 30, 2015 at 1:13
1

Shift+Del and then Ctrl+V

Shift+Del does the cut (same as Ctrl+X) operation on the current line on which cursor is positioned.

Then you can use Ctrl+V to paste it at any place you like.

Good thing is that you don't need to select the complete current line, just the cursor needs to be positioned on the line.

One downside to using this shortcut is that you won't be able to see the line moving ( up or down ) lively in front of your eyes.

0
0

ReSharper's move up and move down shortcuts are smart - instead of just moving the current line (which can easily result in broken code), it will move the current statement up and down in the list of statements that make up the method body. This is much more powerful than just moving a single line of code.

But this doesn't help you get your statement inside the if block. Here you need to do one of two things.

Firstly, move the statement above the if block, then use Ctrl+Shift+Alt+Right to push it down in scope. The statement will now be inside the if block, and you can use up and down to move it around in there. You can also use Ctrl+Shift+Alt+Left to pull it up in scope, outside of the if block.

Alternatively, you can put the text caret on the outside of the closing brace of the if block and use Ctrl+Shift+Alt+Down to move the brace down, stepping over following statements, including them inside the if block.

These keys can do a lot more than just move a single statement. If you put the text caret on a method or class declaration, Up and Down will move the entire method or class declaration up and down. If you put the text caret in the parameter list of a method, Left and Right will rearrange individual parameters (and then Alt+Enter can invoke the Change Signature refactoring).

More details in the help pages.

2
  • 2
    Shortcuts trying to be "smart" is the problem with R#. A lot of times I just need to move a line up or down, nothing to do with the scope or braces.
    – trailmax
    Jan 12, 2017 at 10:43
  • Well, if you're not interested in understanding how the commands you were asking about work, then I can't help. ReShaprer isn't "trying" to be smart, it understands the structure of your code and is helping you rearrange it. Use the editor's own commands for editing plain text. This isn't ReSharper's "problem" - it's giving you tooling, you can either learn it and use it, or use something else. Jan 12, 2017 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.