40

私は Eclipse から Visual Studio .NET に移行していますが、お気に入りのホットキーは 2 つを除いてすべて見つかりました。

  • ALTEclipseでは、 -を押すことができます←</kbd> and ALT-→</kbd> 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-- and CTRL-SHIFT-- 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.?
  • ALTEclipse では、行を上下に移動するには-uparrowまたはALT-を押しdownarrowて、目的の場所に到達するまでコード内を移動するだけです。また、行のコピーを作成するには、-またはSHIFT-を押します。これらのホットキーはどちらも、選択した行のブロックに対しても機能します。ALTuparrowSHIFTALTdownarrow

Visual Studio .NET でこれらのホットキー機能を発見した人はいますか?

補遺 :

上記の 2 番目の機能を使用する場合の例は、ここで一番下の行を for ループに移動することです。Eclipse では、Console.WriteLine にカーソルを置いてから ALT-(上矢印) を押します。私は常にこれを使用しています。1 つのキー ストロークで行を上下に移動します。

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

}
Console.WriteLine(i);

わかりました、行を選択するために no-selection-ctrl-c を使用して Charlie のアイデアを推定すると、Visual Studio でカーソルを Console.WriteLine に置き、(選択なし) を押してCTRL-Xを押し、上に移動してCTRL-を押しVます。

4

9 に答える 9

49

回答は機能を提案しましたが、既存の貼り付けバッファー、現在選択されている文字を保持する方法に関して、Eclipse ほど優れたものはなく、ユーザーが行の範囲を操作することはできません。これは、貼り付けバッファ、現在の文字選択を保持し、選択の有無にかかわらず機能する(複数の行にまたがる場合とそうでない場合がある)、私が思いついたソリューションです。

'' 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)
    DTE.UndoContext.Open("CopyLine")
    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()

    ' copy each line from the top line to the bottom line
    Dim readLine As Long = lTopLine
    Dim endLine As Long = lBottomLine + 1
    Dim selectionPresent As Boolean = ((lTopLine <> lBottomLine) Or (lTopColumn <> lBottomColumn))
    If (selectionPresent And (lBottomColumn = 1)) Then
        ' A selection is present, but the cursor is in front of the first character
        ' on the bottom line. exclude that bottom line from the copy selection.
        endLine = lBottomLine
    End If

    ' figure out how many lines we are copying, so we can re-position
    ' our selection after the copy is done
    Dim verticalOffset As Integer = 0
    If (movingDown) Then
        verticalOffset = endLine - lTopLine
    End If

    ' copy each line, one at a time.
    ' The Insert command doesn't handle multiple lines well, and we need
    ' to use Insert to avoid autocompletions
    Dim insertLine As Long = endLine
    While (readLine < endLine)
        ' move to read postion, and read the current line
        objSel.MoveToLineAndOffset(readLine, 1)
        objSel.EndOfLine(True) 'extend to EOL
        Dim lineTxt As String = objSel.Text.Clone
        ' move to the destination position, and insert the copy
        objSel.MoveToLineAndOffset(insertLine, 1)
        objSel.Insert(lineTxt)
        objSel.NewLine()
        ' adjust the read & insertion points
        readLine = readLine + 1
        insertLine = insertLine + 1
    End While

    ' restore the cursor to original position and selection
    objSel.MoveToLineAndOffset(lBottomLine + verticalOffset, lBottomColumn)
    objSel.MoveToLineAndOffset(lTopLine + verticalOffset, lTopColumn, True)
    DTE.UndoContext.Close()
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()

    Dim textLineAbove As TextSelection = DTE.ActiveDocument.Selection
    textLineAbove.MoveToLineAndOffset(lTopLine - 1, 1, False)
    textLineAbove.MoveToLineAndOffset(lTopLine, 1, True)
    Dim indentChange As Integer = CountIndentations(textLineAbove.Text) * -1

    ' If multiple lines are selected, but the bottom line doesn't
    ' have any characters selected, don't count it as selected
    Dim lEffectiveBottomLine = lBottomLine
    If ((lBottomColumn = 1) And (lBottomLine <> lTopLine)) Then
        lEffectiveBottomLine = lBottomLine - 1
    End If

    ' 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 >= lEffectiveBottomLine)
    ' Since the line we are on has moved up, our location in the file has changed:
    lTopLine = lTopLine - 1
    lBottomLine = lBottomLine - 1

    IndentBlockAndRestoreSelection(objSel, lBottomLine, lBottomColumn, lTopLine, lTopColumn, indentChange)

    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()

    ' If multiple lines are selected, but the bottom line doesn't
    ' have any characters selected, don't count it as selected
    Dim lEffectiveBottomLine = lBottomLine
    If ((lBottomColumn = 1) And (lBottomLine <> lTopLine)) Then
        lEffectiveBottomLine = lBottomLine - 1
    End If

    Dim textLineBelow As TextSelection = DTE.ActiveDocument.Selection
    textLineBelow.MoveToLineAndOffset(lEffectiveBottomLine + 1, 1, False)
    textLineBelow.MoveToLineAndOffset(lEffectiveBottomLine + 2, 1, True)
    Dim indentChange As Integer = CountIndentations(textLineBelow.Text)


    ' move to the bottom line
    objSel.MoveToLineAndOffset(lEffectiveBottomLine, 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 = lEffectiveBottomLine - 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

    IndentBlockAndRestoreSelection(objSel, lBottomLine, lBottomColumn, lTopLine, lTopColumn, indentChange)

    DTE.UndoContext.Close()
End Sub

'' This method takes care of indenting the selected text by the indentChange parameter
'' It then restores the selection to the lTopLine:lTopColumn - lBottomLine:lBottomColumn parameter.
'' It will adjust these values according to the indentChange performed
Sub IndentBlockAndRestoreSelection(ByVal objSel As TextSelection, ByVal lBottomLine As Long, ByVal lBottomColumn As Long, ByVal lTopLine As Long, ByVal lTopColumn As Long, ByVal indentChange As Integer)
    ' restore the cursor to original position and selection
    objSel.MoveToLineAndOffset(lBottomLine, lBottomColumn)
    objSel.MoveToLineAndOffset(lTopLine, lTopColumn, True)
    If (indentChange = 0) Then
        ' If we don't change the indent, we are done
        Return
    End If

    If (lBottomLine = lTopLine) Then
        If (indentChange > 0) Then
            objSel.StartOfLine()
        Else
            objSel.StartOfLine()
            objSel.WordRight()
        End If
    End If
    objSel.Indent(indentChange)

    ' Since the selected text has changed column, adjust the columns accordingly:
    ' restore the cursor to original position and selection
    Dim lNewBottomColumn As Long = (lBottomColumn + indentChange)
    Dim lNewTopColumn As Long = (lTopColumn + indentChange)
    ' ensure that we we still on the page.
    ' The "or" clause makes it so if we were at the left edge of the line, we remain on the left edge.
    If ((lNewBottomColumn < 2) Or (lBottomColumn = 1)) Then
        ' Single line selections, or a bottomColumn that is already at 1 may still have a new BottomColumn of 1
        If ((lTopLine = lBottomLine) Or (lBottomColumn = 1)) Then
            lNewBottomColumn = 1
        Else
            ' If we have multiple lines selected, don't allow the bottom edge to touch the left column,
            ' or the next move will ignore that bottom line.
            lNewBottomColumn = 2
        End If
    End If
    If ((lNewTopColumn < 2) Or (lTopColumn = 1)) Then
        lNewTopColumn = 1
    End If

    ' restore the selection to the modified selection
    objSel.MoveToLineAndOffset(lBottomLine, lNewBottomColumn)
    objSel.MoveToLineAndOffset(lTopLine, lNewTopColumn, True)
End Sub


'' This method counts the indentation changes within the text provided as the paramter
Function CountIndentations(ByVal text As String) As Integer
    Dim indent As Integer = 0
    While (Text.Length > 0)
        If (Text.StartsWith("//")) Then
            Dim endOfLine As Integer = Text.IndexOf("\n", 2)
            If (Equals(endOfLine, -1)) Then
                ' The remaining text is all on one line, so the '//' terminates our search
                ' Ignore the rest of the text
                Exit While
            End If
            ' continue looking after the end of line
            Text = Text.Substring(endOfLine + 1)
        End If

        If (Text.StartsWith("/*")) Then
            Dim endComment As Integer = Text.IndexOf("*/", 2)
            If (Equals(endComment, -1)) Then
                ' This comment continues beyond the length of this line.
                ' Ignore the rest of the text
                Exit While
            End If
            ' continue looking after the end of this comment block
            Text = Text.Substring(endComment + 1)
        End If

        If (Text.StartsWith("{")) Then
            indent = indent + 1
        Else
            If (Text.StartsWith("}")) Then
                indent = indent - 1
            End If
        End If
        Text = Text.Substring(1)
    End While
    Return indent
End Function

この投稿を編集して、MoveLineUp() メソッドと MoveLineDown() メソッドの先頭に UndoContext メカニズム (Nicolas Dorier が提案) を追加し、最後にそれを閉じました。2011 年 11 月 23 日 - これを再度更新して、括弧の境界を越えるときに移動した行がインデントできるようにしました。

于 2009-06-10T00:08:52.317 に答える
16

Visual Studio 2010でこれを行う方法を探している人のために、無料のVisual Studio 2010 Pro Power Tools拡張機能により、行を上下に移動する機能が追加されます。

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

于 2010-07-13T19:14:02.030 に答える
14

まだ見つからない場合、これらのキーボード ショートカットが設定されている場所は、[ツール] | [ツール] にあります。オプション | 環境 | 環境 | キーボード。リストをブラウズするだけで、多くの便利なコマンドを見つけることができますが、残念ながら、各コマンドの目的を説明するための適切なリファレンスは見つかりませんでした。

特定のコマンドについては、次のとおりです。

  • あなたが参照している前後のナビゲーション コマンドは、View.NavigateBackward と View.NavigateForward だと思います。キーボードが VS キー バインドと連携していない場合は、それらを好みの Eclipse キーに再マップできます。残念ながら、実際に移動先を決定するために使用するアルゴリズムを変更する方法はわかりません。

  • 行を複製するための組み込みコマンドはないと思いますが、テキストを選択せず​​に Ctrl+C を押すと、現在の行がクリップボードにコピーされます。それを考慮して、現在の行を次の行に複製する単純なマクロを次に示します。


    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
  • テキスト行を移動する場合、Edit.LineTranspose は選択した行を下に移動します。行を上に移動するコマンドはないと思いますが、これを行う簡単なマクロを次に示します。

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

まだマクロを使い始めていない場合、マクロは非常に便利です。ツール | マクロ | Macros IDE からエディターが表示され、定義が完了すると、上記と同じ UI を使用してキーボード ショートカットを設定できます。これらのマクロは、非常に便利な一時マクロの記録コマンドを使用して生成しました。マクロ。このコマンドを使用すると、一連のキーボード入力を記録し、それらを何度でも再生できます。これは、高度な編集コマンドを作成したり、反復タスク (コードの再フォーマットなど) を自動化するのに適しています。

于 2008-11-24T05:16:35.453 に答える
8

私は最近同じことを行い、新しいプロジェクトに移行したときに Eclipse から Visual Studio に移行しました。 Resharper アドインを強くお勧めしますこれにより、Eclipse が VS に持つ豊富な編集、ナビゲーション、およびリファクタリング機能の一部が追加されます。

Resharper では、InteliJ と非常によく似たキーボード マッピング スキームを使用することもできます。Javaエスケープにとって非常に便利です...

2 番目の質問について、Resharper には eclipse と同じムーブ コード アップ/ダウン機能がありますが、いくつかの注意点があります。まず、InteliJ キーボード マッピングを使用すると、キーの組み合わせがやや複雑になります。

コードを上に移動: ctrl + shift + alt + 上カーソル

コードを下に移動: ctrl + shift + alt + 下カーソル

次に、常に 1 行だけ移動するとは限らず、実際にはコード ブロックをジャンプします。そのため、行を if ステートメントの外側から内側に移動することはできません。選択した行を if ブロックのすぐ上にジャンプします。そのためには、「左」と「右」を使用して移動する必要があります

コードを外側のコード ブロックに移動: ctrl + shift + alt + 左カーソル

コードを次の内部コード ブロックに移動: ctrl + shift + alt + 右カーソル

于 2008-11-24T12:51:42.867 に答える
2

Visual Studioでマクロを記録して、代替矢印を実行します。

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

これで、マクロideとキーボード設定を使用して、このマクロを任意のキーストロークのセットにマップできます。

于 2008-11-24T05:33:22.067 に答える
2

Edit.LineTranspose しかし、これは行を上に移動するには機能しません... 行を上に移動するマクロは次のとおりです

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
于 2009-04-11T14:00:33.957 に答える
1

MoveLine 拡張機能を使用して、VS 2010/2012 で行 (または行のグループ) を上下に移動します。

于 2012-10-03T12:32:22.520 に答える
0

VS があなたが話している機能をネイティブにサポートしているかどうかはわかりませんが、resharper プラグインを使用すると、CTRL + SHIFT + BACKSPACE を使用して以前の編集に移動できることはわかっています。行を上下に移動することはサポートされていないと思います(まだ見つけたわけではありません)

于 2008-11-24T05:14:56.703 に答える
0

Paul Ostrowski 私はあなたのツールを試しました。それはほとんど問題なく動作します。

Eclipse が行うもう 1 つのことは、行を現在のインデント レベルに移動することです。

例えば:

function test()
{
    // do stuff
}
Console.WriteLine("test"); 

console.writeline でシフトアップを行うと、次のように変更されます

function test()
{
    // do stuff
    Console.WriteLine("test"); 
}

しかし、あなたのツールはこれを行うようです:

function test()
{
    // do stuff
Console.WriteLine("test"); 
}
于 2011-05-07T06:21:28.593 に答える