Visual Studio 代码中的多行编辑

Multiline editing in Visual Studio Code

是否可以像Sublime Text那样启用多行编辑?

例如,按 Ctrl 可以放置额外的光标插入符,并且能够一次 write/delete 在文档的多个位置。

在 Windows 上,您按住 Ctrl+Alt 的同时按向上 或向下 箭头键添加光标。

Mac: ⌥ Opt+⌘ Cmd+/

Linux: Shift+Alt+/

Note that third-party software may interfere with these shortcuts, preventing them from working as intended (particularly Intel's HD Graphics software on Windows; see comments for more details).

If you experience this issue, you can either disable the Intel/other software hotkeys, or modify the VS Code shortcuts (described below).

Esc 重置为单个光标。

或者,,您可以按住Alt并左键单击任意放置光标。

您可以通过以下方式查看和编辑键盘快捷键:

File → Preferences → Keyboard Shortcuts

文档:

https://code.visualstudio.com/docs/customization/keybindings

官方 VS Code 键盘快捷键备忘单:

https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf
https://code.visualstudio.com/shortcuts/keyboard-shortcuts-macos.pdf
https://code.visualstudio.com/shortcuts/keyboard-shortcuts-linux.pdf

您只需 Alt + 单击即可获得更多光标。如前所述,Ctrl + Alt + .

在 Mac 上是:

Option + Command 同时按下向上 或向下 方向键。

(Windows 10 pro x64) 这里有一些方法!

  1. Alt + 单击

  2. Alt + Ctrl + up/向下

  3. 键绑定:Ctrl + click(???它不起作用!)

我认为这取决于您的 Visual Studio 代码版本。

我的是 Linux 版本 Visual Studio 代码 1.7.2.

{ "key": "ctrl+shift+up",         "command": "editor.action.insertCursorAbove",
                                     "when": "editorTextFocus" },
{ "key": "shift+alt+up",          "command": "editor.action.insertCursorAbove",
                                     "when": "editorTextFocus" },
{ "key": "ctrl+shift+down",       "command": "editor.action.insertCursorBelow",
                                     "when": "editorTextFocus" },
{ "key": "shift+alt+down",        "command": "editor.action.insertCursorBelow",
                                     "when": "editorTextFocus" }

关键是所有机器的快捷方式都不一样,所以你应该检查你的配置。转到菜单:

Menu FilePreferencesKeyboard Shortcuts

搜索 editor.action.insertCursorAboveeditor.action.insertCursorBelow 并查看您当前的配置。如果它们与操作系统的快捷键冲突,您可以更改它们。

方框选择

Windows: shift + alt + 鼠标左键

macOS:shift + option + Click

这与an answer to Does Visual Studio Code have box select/multi-line edit?中提到的相反。

我正在使用 vscodevim 扩展程序,所以我不确定这是否是一个常见问题。但是,我遇到了 Ctrl + Alt + UpArrow 翻转我的屏幕的问题。

查看 Visual Studio Code Basics(我不知道他们是否在最近的更新中更改了它),它说使用:

Ctrl + Alt + Shift + (Up/向下)

version 1.13 (May 2017) 您终于可以更改用于创建多个游标的默认修饰键(添加到设置):

"editor.multiCursorModifier": "ctrlCmd"

P.S.: 从这一刻起修饰符 "follow link" 将是 A​​lt.

两步解决!

  1. Ctrl + F

  2. Alt + Enter

相当于Sublime Text的Alt + F3.

如果您使用 Linux,可能会与 Alt + click 发生冲突,这是默认为 "moving a window"。

您可以转到菜单设置Window 行为Window 行为操作 选项卡

只需删除 Alt + left(按住)即可。

这是最好的方法,因为您不需要按住两个 + 键来完成这么简单的任务。

使用Ctrl + D对Windows和Linux中的相同单词进行多词编辑。

使用CMD + D Mac.

在 Visual Studio 代码中,只需按 Alt 并将光标置于编辑位置(您要编辑的位置),然后右键单击 select。

我想 select 多行并点击“某物”为每个 select 行设置一个光标(类似于 Ctrl + Shift + L 在 Sublime Text 中)。 Visual Studio 代码中的此操作称为“将光标添加到行尾”。

这已在 Visual Studio 代码 1.51.1 中测试并适用于 WindowsMac.

方法如下:

  1. Select 你希望有多个光标的行。
  2. 只需按 Alt + Shift-I.

您现在每 selected 行有一个光标。

As of April 2018 (version 1.23) 您现在也可以使用鼠标中键来多行 select / 框 select.

第 1 步:

Select 要替换的单词。


第 2 步:

使用 Ctrl + F 到 select 它的多次出现。


第 3 步:

使用 Alt + Enter 将光标设置在所有找到的位置。


第 4 步:

只需开始输入新单词。


或者 Mac:

⌥ Opt + Click 将光标设置在点击位置。

在 Windows 中,以下组合对我有用:

  • Ctrl + Shift + Alt + 向下箭头
  • Ctrl + Shift + Alt + 向上箭头

我正在使用 Sublime Text 键映射,而最佳答案提供的键绑定似乎不起作用:( Visual Studio 代码和 sublime 键映射之间可能存在一些冲突。

推荐的键绑定 by @Han 对我有用(非常感谢!):

  • 使用 Ctrl + Shift + Up/[=24= 进入多行光标模式]向下
  • 使用 Esc
  • 退出

(旁注)下面是将 Emmet 与多行光标(使用上面列出的这些键绑定启用和禁用)一起使用的小示例:

只需 Opt + 点击 Mac

在最新版本的 Visual Studio 代码中,您现在可以在按住 Option 的同时拖动光标(Alt on Windows) 到 select 多行的同一列。

要启用此功能,请确保将 editor.multiCursorModifier 更改为如下所示:

"editor.multiCursorModifier": "ctrlCmd"

来自 Visual Studio 代码发行说明 1.32.0:

In the following video, the selection begins as a regular selection and then Alt is pressed and held until the mouse button is released:

我的设置:Windows8.1 64位,Visual Studio代码版本1.33.1。

问题:与 Intel HD Graphics 4000 的键盘快捷键冲突

我在使用 Visual Studio 代码 (Ctrl + Alt + UP, Ctrl + Alt + DOWN)。在我的例子中,这些命令分别将我的屏幕旋转 0º 和 180º。它正在发生,因为我的电脑上安装了 Graphic Intel 应用程序。然后,我只是禁用了这个应用程序的快捷方式。

Here 是帮助某人的教程。

除了所有的答案,还有一个方法。 Select 你想要的行然后按:

  • Windows: Shift + A​​lt + i
  • Mac: shift + option + i

这会在所选内容的每一行中放置一个光标。

当我需要快速编辑文件中的 ~20k 行时,使用 SHIFT + ALT + i 对我有用。提到的大多数其他键盘快捷键仅对有限数量的行有用。 SHIFT 选择要进行列编辑的区域,然后按 SHIFT + ALT + i 是处理此问题的最有效方法。

不过我无意中发现(此处尚未提及)"cursor edit mode"[=33= 中有 10k 行的限制](有 an open Github feature request 来增加它)。这意味着对于非常大的文件,编写编辑脚本可能比手动执行更好。

对于那些像 那样必须使用像 CTRL + SHIFT + L 这样的快捷方式(默认绑定是 Select all occurrences of current selection)来完成此操作的人,你应该检查你没有键盘映射扩展安装。您可以通过在扩展窗格中搜索 @recommended:keymaps 进行检查,转到 File > Preferences > Keymaps (CTRL+K CTRL+M).

可以在 PDF 格式 HERE, or you can go to File > Preferences > Keyboard Shortcuts to find/modify the current bindings. There is also more in-depth key bindings documentation HERE 中找到默认的 [Windows] 键盘映射 HERE, or you can go to File > Preferences > Keyboard Shortcuts to find/modify the current bindings. There is also more in-depth key bindings documentation HERE

我的工作解决方案是首先select编辑所需的文本,然后使用CTRL + F2到select页面中所有匹配的数据。 您也可以按照 [=13 的建议使用 CTRL+Shift+L =]

Please Note: The above solution uses the inherent ability of VSCode editor to select similar text across the entire page, and therefore, be careful.

对我来说,上述 Ctrl + ALT + Arrowkeys 的解决方案不起作用它导致屏幕根据 selecting VSCode.

中的行更改其显示方向

version 1.43 (February 2020)

您现在可以通过以下方式切换列选择模式,该模式会更改鼠标手势和箭头键:

  • 菜单栏 > 选择 > 列选择模式
  • Ctrl+Shift+P(显示所有命令)> 切换列选择模式
  • 为命令绑定密钥 "editor.action.toggleColumnSelection"

注意:激活后状态栏中有一个“列选择”面板,您可以再次按下该面板将其禁用。

在带有 KDE 的 Linux Fedora(我试过 Fedora 31)上,转到:

  1. System Settings
  2. Window Management
  3. Window Behavior
  4. Window Actions
  5. Inner Window, Titlebar and Frame Actions
  6. Click Left Click Box
  7. Pick Do nothing (replacing move/drag)

现在您可以 select 多行 Alt + 左键单击

我在 Windows 10 中使用最新版本的 VS 代码,即 1.46.1(2020 年 5 月)。只需在您想要的行上按 Alt+mouse left click select。这将让您一次 select 多行并让您编辑它们。同时按 Esc 退出。 如果您来自 sublime text,这真的很有帮助。

对我来说 Alt + Middle Click(滚轮)效果很好 您必须单击 Alt,然后长按 Middle Click,然后滚动 Updown

(没有鼠标)对于 macOS,我发现这非常快!

  1. CMD + f 搜索要更改的(单词)。
  2. Option + Enter 至 select 您搜索的所有单词。

只需更新第一个单词,它将更新所有 selected。

在 Windows..

  1. Select一行你要到处改。 (用鼠标光标)
  2. CTRL + F2,您将立即开始编辑所有行

windows 上,您可以通过按以下方式在 next/previous 行上创建附加光标:

Ctrl + Alt + Shift +向下/向上

缺少 Shift 会翻转屏幕。

创建额外的光标将允许您一次编辑多行。

根据 VS Code 1.55.2 版本 (2021) 在 User > Commonly Used 部分有一个选项 Multi Cursor Modifier。见下图。

for multi cursor 默认键是 alt 如果你想要它可以更改为 Ctrl。 另外,对于合适的地方,可以按住Middle Mouse button向下移动

在任意文本位置添加多个光标

这是通过使用 Ctrl+Click 完成的。首先,您必须启用 Multiple-Cursor Modifier。只需通过 Ctrl+Shift+P[= 在 Command Palette 上键入此选项27=] 启用它。

从多个文本位置删除光标。

在光标上按 Ctrl+单击