I was searching for a duplicate current line in visual studio like notepad++ without override the clipboard.
Well in the fantaastic sit stackoverflow.com I found a complee discussion about it.
My fav was the 2nd post as answer. I am reporting here because I want to share it and I don't want to loose it.
Starting from here is the original post.
// QUOTE
For those interested, I recreated the exact behaviour of Notepad++ into a Visual Studio Macro. Below you can find the code (extensively commented). If you want to have a shortcut like in Notepad++ (Ctrl + D), you can easily do this by going to Tools > Option > Environment > Keyboard and search for Macros.MyMacros.DuplicateText.DuplicateText. Select the command and assign the shortcut (I recommend assigning it only to the Text Editor and not globally).
For those who don't know how to add this macro to Visual Studio, below the explanation how to do this:
- Go to
Tools,Macros,Macro Explorer(or use the shortcur Alt + F8) - Now in the Macro Explorer, right click on
MyMacrosand chooseNew Module - A new windo opens and give it the name
DuplicateText - Now open this module by double clicking it. A new window (
Microsoft Visual Studio Macros) opens. - Now delete all the text you see and paste the code below in there.
Here is the code
Imports System
Imports EnvDTE
Imports EnvDTE80
Public Module DuplicateText
' Notepad++ duplication of text
' If no text is selected, duplicate the current line on the next line and leave the cursor
' at the current position.
' In all other cases, duplicate the selected text and insert it after the selection (without
' inserting a new line), leaving the selection intact
Sub DuplicateText()
Dim selTextSelection As TextSelection = DTE.ActiveDocument.Selection
' Retrieve the current cursor position
Dim iActiveLine As Integer = selTextSelection.ActivePoint.Line
Dim iActiveLineCharOffset As Integer = selTextSelection.ActivePoint.LineCharOffset
' Retrieve the selection start position
Dim iStartLine As Integer = selTextSelection.TopPoint.Line
Dim iStartLineCharOffset As Integer = selTextSelection.TopPoint.LineCharOffset
' Retrieve the selection end position
Dim iEndLine As Integer = selTextSelection.BottomPoint.Line
Dim iEndLineCharOffset As Integer = selTextSelection.BottomPoint.LineCharOffset
If selTextSelection.IsEmpty Then
''' Case when no text is selected
' Select the current line and insert it after this line
selTextSelection.SelectLine()
selTextSelection.Insert(selTextSelection.Text, vsInsertFlags.vsInsertFlagsInsertAtEnd)
' Move the cursor back to the original position
selTextSelection.MoveToLineAndOffset(iActiveLine, iActiveLineCharOffset)
Else
''' Case when there is a selection
' Insert the selected text after the selection
selTextSelection.Insert(selTextSelection.Text, vsInsertFlags.vsInsertFlagsInsertAtEnd)
' Recreate the original selection
' > Determine from which point the selection has to start
' >>> If the active point is at the start, start selecting from the end point,
' >>> else if the active is at the end, start selecting from the start point
Dim iFromLine As Integer = If(iActiveLine = iStartLine, _
iEndLine, _
iStartLine)
Dim iFromCharOffset As Integer = If(iActiveLineCharOffset = iStartLineCharOffset, _
iEndLineCharOffset, _
iStartLineCharOffset)
' > Move the cursor temporary to the point where you start selecting (see above)
selTextSelection.MoveToLineAndOffset(iFromLine, iFromCharOffset)
' > Move the cursor to the (original) active point while selecting the text
selTextSelection.MoveToLineAndOffset(iActiveLine, iActiveLineCharOffset, True)
End If
End Sub
End Module
// UNQUOTE
-have fun-

