views:

3815

answers:

8

I'm moving from Eclipse to Visual Studio .NET and have found all my beloved hotkeys except two:

  • in Eclipse you can press ALT-(leftarrow) and ALT-(rightarrow) to visit recent changes you have made, something I use frequently to go back to where I was in some other file and then return. Apparently in VS.NET the CTRL-(minus) and CTRL-SHIFT-(minus) do this but they don't seem to always work (e.g. on laptop, may be a numkey issue with the minus) and don't seem to follow the same algorithm of "where I was" as I am used to in Eclipse. Has anyone gotten this to work and rely on it daily, etc.?
  • in Eclipse, to move a line up or down you press ALT-(uparrow) or ALT-(downarrow) and you just move it through the code until you get it to where you want it, very nice. Also to make a copy of a line, you can press SHIFT-ALT-(uparrow) or SHIFT-ALT-(downarrow). Both of these hotkeys even work for block of lines that you have selected.

Has anyone discovered these hotkey features in Visual Studio .NET?

A D D E N D U M :

An example of when you would use the second feature described above is to move the bottom line here up into the for loop. In Eclipse, you would put the cursor on the Console.WriteLine and then press ALT-(uparrow), I use that all the time: one key stroke to move lines up and down.

for (int i = 0; i < 10; i++) {

}
Console.WriteLine(i);

Ok, extrapolating Charlie's idea with no-selection-ctrl-c to select a line, in Visual Studio you could put your cursor on Console.WriteLine, (no selection) press CTRL-X and then move up and press CTRL-V.

A: 

I don't know if VS supports the features you're talking about natively, but I know the resharper plugin allows you to go to the previous edits by using CTRL + SHIFT + BACKSPACE. I don't think it has support for moving a line up and down tho (well not that I've found yet)

lomaxx
It kind of does have the move line up and down shortcut, but it doesn't work exactly like in eclipse - see my answer below.
serg10
+7  A: 

If you haven't already found it, the place where these keyboard shortcuts are setup is under Tools | Options | Environment | Keyboard. A lot of handy commands can be found just by browsing through the list, although unfortunately I've never found any good reference for describing what each command is intended to do.

As for specific commands:

  • I believe the forward/backward navigation commands you're referring to are View.NavigateBackward and View.NavigateForward. If your keyboard isn't cooperating with the VS key bindings, you can remap them to your preferred Eclipse keys. Unfortunately, I don't know of a way to change the algorithm it uses to actually decide where to go.

  • I don't think there's a built-in command for duplicating a line, but hitting Ctrl+C with no text selected will copy the current line onto the clipboard. Given that, here's a simple macro that duplicates the current line on the next lower line:


    Sub CopyLineBelow()
        DTE.ActiveDocument.Selection.Collapse()
        DTE.ActiveDocument.Selection.Copy()
        DTE.ActiveDocument.Selection.Paste()
    End Sub

    Sub CopyLineAbove()
        DTE.ActiveDocument.Selection.Collapse()
        DTE.ActiveDocument.Selection.Copy()
        DTE.ActiveDocument.Selection.LineUp()
        DTE.ActiveDocument.Selection.Paste()
    End Sub
  • For moving a line of text around, Edit.LineTranspose will move the selected line down. I don't think there's a command for moving a line up, but here's a quick macro that does it:

    Sub MoveLineUp()
        DTE.ActiveDocument.Selection.Collapse()
        DTE.ActiveDocument.Selection.Cut()
        DTE.ActiveDocument.Selection.LineUp()
        DTE.ActiveDocument.Selection.Paste()
        DTE.ActiveDocument.Selection.LineUp()
    End Sub

If you haven't yet started playing with macros, they are really useful. Tools | Macros | Macros IDE will take you the editor, and once they're defined, you can setup keyboard shortcuts through the same UI I mentioned above. I generated these macros using the incredibly handy Record Temporary Macro command, also under Tools | Macros. This command lets you record a set of keyboard inputs and replay them any number of times, which is good for building advanced edit commands as well as automating repetitive tasks (e.g. code reformatting).

Charlie
The CTRL-C, CTRL-V with no selection to copy a line is nice, didn't know that one, thanks.
Edward Tanguay
A: 

Record a macro in visual studio to do the alt-arrow thing:

ctrl-alt-r -- record mode
ctrl-c -- copy a line
up arrow -- go up a line
home -- beginning of line (maybe there is a way to paste before the current line without this)
ctrl-v -- paste
ctrl-alt-r -- end record mode

Now you can map this macro to any set of keystrokes you like using the macros ide and the keyboard preferences.

1800 INFORMATION
+3  A: 

If you'll excuse the self promotion, I blogged about some of the more useful Visual Studio shortcuts here.

Sara Ford has a blog devoted to them here.

You can download a printable shortcut reference poster from here.

Mitch Wheat
If you're going to link out to other sites, you still need to answer the question in your answer. Bad etiquette.
Catskul
@Catskul: you mean despite the fact it was the accepted answer and poster found it useful? Thx for the downvote
Mitch Wheat
per my understanding, currently, there is no functionality to move a line up or down and therefore no hotkey can be applied. Right?
Dimitris Baltas
+3  A: 

I have recently done the same thing and moved from Eclipse to Visual Studio when I moved onto a new project. The Resharper add in is highly recommended - it adds some of the rich editing, navigational and refactoring functionality that eclipse has to VS.

Resharper also allows you to use a keybaord mapping scheme that is very simillar to InteliJ. Very handy for the Java escapees...

Regarding your second question, Resharper has the same move code up / down function as eclipse, but with some caveats. Firstly, using the InteliJ keyboard mappings, the key combination is rather tortuous.

Move code up: ctrl + shift + alt + up cursor

Move code down: ctrl + shift + alt + down cursor

Secondly, it does not always move by just one line, but actually jumps code blocks. So it cannot move a line from outside an if statement to inside it - it jumps the selected line right over the if block. To do that you need to move "left" and "right" using

Move code into outer code block: ctrl + shift + alt + left cursor

Move code into next inner code block: ctrl + shift + alt + right cursor

serg10
A: 

Edit.LineTranspose but this doesn't work to move a line up... Here is the macro to move line up

Sub LineTransposeUp()
    Dim offset As Integer
    Dim sel As TextSelection

    DTE.UndoContext.Open("LineTransposeUp")

    Try
        sel = DTE.ActiveDocument.Selection
        offset = sel.ActivePoint.LineCharOffset
        sel.LineUp()
        DTE.ExecuteCommand("Edit.LineTranspose")
        sel.LineUp()
        sel.MoveToLineAndOffset(sel.ActivePoint.Line, offset)
    Catch ex As System.Exception
    End Try

    DTE.UndoContext.Close()
End Sub
Nicolas Dorier
+8  A: 

The answers proposed work, but none of them are as nice as eclipse with regard to how they preserve the existing paste buffer, the currently selected characters, and they do not allow the user to operate upon a range of lines. Here is a solution I came up with that preserves the paste buffer, the current character selection, and works with or without a selection (that may or may not span multiple rows):

'' Duplicates the current line (or selection of lines) and places the copy
'' one line below or above the current cursor position (based upon the parameter)
Sub CopyLine(ByVal movingDown As Boolean)
    Dim objSel As TextSelection = DTE.ActiveDocument.Selection
    ' store the original selection and cursor position
    Dim topPoint As TextPoint = objSel.TopPoint
    Dim bottomPoint As TextPoint = objSel.BottomPoint
    Dim lTopLine As Long = topPoint.Line
    Dim lTopColumn As Long = topPoint.LineCharOffset
    Dim lBottomLine As Long = bottomPoint.Line
    Dim lBottomColumn As Long = bottomPoint.LineCharOffset()
    Dim verticalOffset As Integer = 0
    If (movingDown) Then
        verticalOffset = (lBottomLine - lTopLine) + 1
    End If

    If ((lTopLine <> lBottomLine) Or (lTopColumn <> lBottomColumn)) Then
        ' A selection is present. Select all lines in their entirety
        objSel.MoveToLineAndOffset(lBottomLine, 1)
        objSel.MoveToLineAndOffset(lTopLine, lTopColumn, True)
    Else
        ' No characters are selected, use the enitre current line
        objSel.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
    End If
    ' always extend the selection to the end of the lower line
    objSel.EndOfLine(True)

    Dim linetext As String = objSel.Text
    objSel.EndOfLine() ' de-select the current selection

    ' make the duplicate after the current selection:
    objSel.NewLine()
    objSel.Text = linetext

    ' restore the cursor to original position and selection
    objSel.MoveToLineAndOffset(lBottomLine + verticalOffset, lBottomColumn)
    objSel.MoveToLineAndOffset(lTopLine + verticalOffset, lTopColumn, True)
End Sub

'' Duplicates the current line (or selection of lines) and places the copy
'' one line below the current cursor position
Sub CopyLineDown()
    CopyLine(True)
End Sub

'' Duplicates the current line (or selection of lines) and places the copy
'' one line above the current cursor position
Sub CopyLineUp()
    CopyLine(False)
End Sub

'' Moves the selected lines up one line. If no line is
'' selected, the current line is moved.
''
Sub MoveLineUp()
    DTE.UndoContext.Open("MoveLineUp")
    Dim objSel As TextSelection = DTE.ActiveDocument.Selection
    ' store the original selection and cursor position
    Dim topPoint As TextPoint = objSel.TopPoint
    Dim bottomPoint As TextPoint = objSel.BottomPoint
    Dim lTopLine As Long = topPoint.Line
    Dim lTopColumn As Long = topPoint.LineCharOffset
    Dim lBottomLine As Long = bottomPoint.Line
    Dim lBottomColumn As Long = bottomPoint.LineCharOffset()

    ' move to the line above the top line
    objSel.MoveToLineAndOffset(lTopLine - 1, 1)
    ' and move it down, until its below the bottom line:
    Do
        DTE.ExecuteCommand("Edit.LineTranspose")
    Loop Until (objSel.BottomPoint.Line >= lBottomLine)
    ' Since the line we are on has moved up, our location in the file has changed:
    lTopLine = lTopLine - 1
    lBottomLine = lBottomLine - 1

    ' restore the cursor to original position and selection
    objSel.MoveToLineAndOffset(lBottomLine, lBottomColumn)
    objSel.MoveToLineAndOffset(lTopLine, lTopColumn, True)
    DTE.UndoContext.Close()
End Sub

'' Moves the selected lines down one line. If no line is
'' selected, the current line is moved.
''
Sub MoveLineDown()
    DTE.UndoContext.Open("MoveLineDown")
    Dim objSel As TextSelection = DTE.ActiveDocument.Selection
    ' store the original selection and cursor position
    Dim topPoint As TextPoint = objSel.TopPoint
    Dim bottomPoint As TextPoint = objSel.BottomPoint
    Dim lTopLine As Long = topPoint.Line
    Dim lTopColumn As Long = topPoint.LineCharOffset
    Dim lBottomLine As Long = bottomPoint.Line
    Dim lBottomColumn As Long = bottomPoint.LineCharOffset()

    ' move to the bottom line
    objSel.MoveToLineAndOffset(lBottomLine, 1)
    ' and move it down, which effectively moves the line below it up
    ' then move the cursor up, always staying one line above the line
    ' that is moving up, and keep moving it up until its above the top line:
    'Dim lineCount As Long = lTopLine - lBottomLine
    Dim lineCount As Long = lBottomLine - lTopLine
    Do
        DTE.ExecuteCommand("Edit.LineTranspose")
        objSel.LineUp(False, 2)
        lineCount = lineCount - 1
    Loop Until (lineCount < 0)
    ' Since the line we are on has moved down, our location in the file has changed:
    lTopLine = lTopLine + 1
    lBottomLine = lBottomLine + 1

    ' restore the cursor to original position and selection
    objSel.MoveToLineAndOffset(lBottomLine, lBottomColumn)
    objSel.MoveToLineAndOffset(lTopLine, lTopColumn, True)
    DTE.UndoContext.Close()
End Sub

I edited this post to add the UndoContext mechanism (suggested by Nicolas Dorier) at the beginning of the MoveLineUp() and MoveLineDown() methods and closing it at their end.

Paul Ostrowski
That is awesome. I wish I could vote it up more than once. Great job!
Matt Blaine
This should be the answer, not the Mitch Wheat's one. Great job!
Borek
Actually, this script doesn't work when I select lines "lazily", i.e. I start selection somewhere in the middle of line 1 and end somewhere in the middle of the last line. Eclipse handles this perfectly.
Borek
+3  A: 

For anyone looking for a way to do this in Visual Studio 2010, the free Visual Studio 2010 Pro Power Tools extension adds the capability to move lines up and down.

http://visualstudiogallery.msdn.microsoft.com/en-us/d0d33361-18e2-46c0-8ff2-4adea1e34fef

Polshgiant