See that line of code up there? I need it here as well.

It happens all the time, you’re writing a new code segment and see the line you’re about to write (or close enough version of it) a few lines above your cursor.

If you’re anything like me – you have your line numbers always displayed so you can tell which line it is that you’re after; you hit Ctrl·G, type in the line number, select the text once you get there and go back to the line you were editing using Ctrl·-, Ctrl·G or the arrow keys, depending on your mood; now you just need to paste the code back. A lot of time wasted here.

If you’re not so much like me – you’ll use the mouse and spend a little less time (in this specific case).

So, why spend time at all? Let’s automate baby!

Take this macro and paste it in your Macro explorer in VS2003 (might also work in VS2005 but I haven’t tested it just yet):

CopyLine macro for Visual Studio 2003 (and later?)
Sub CopyLine()
source = DTE.ActiveDocument.Selection.TopPoint.Line
line = InputBox("Line number to copy here:")
If (line <> "") Then
DTE.ActiveDocument.Selection.GotoLine(line)
DTE.ActiveDocument.Selection.EndOfLine(True)
DTE.ActiveDocument.Selection.Copy()
DTE.ActiveDocument.Selection.GotoLine(source)
DTE.ActiveDocument.Selection.Paste()
End If
End Sub

I assigned Ctrl·Alt·Shift·C to this macro so my line copying is just 2-4 clicks away (if your line numbers reach 4 digits or more, you have more serious problems…)

You can copy this to another macro and replace the Copy( ) command with Cut( ) so you’ll also have a line moving macro. :-)

Published in:  on August 22, 2007 at 2:33 Leave a Comment