是否有任何技巧或扩展来选择visual studio代码中所选单词的所有实例,以方便编辑或删除这些实例而无需搜索和替换,如sublime text中的ِAlt+F3


当前回答

已经列出了几个选项,但有两个没有。它可以使用重命名和重构工具,不只是选择所有,但做出特定的改变,在所有已经选择。我将尝试将所有我认为相关的答案捆绑在一起,并添加两个以上的答案,不仅可以完成工作,而且是在同一代码多次出现时进行单一更改的非常好的工具。



1. 选择所有匹配


若要使用多个游标来选择所有匹配项,可以使用2个键绑定中的1个。两个键绑定执行相同的功能,换句话说,2个键绑定,1个vscode命令。按键绑定如下。

1. CTRL + f2

2. CTRL + shift + l

默认Keybinding

{
  "key": "ctrl+f2",
  "command": "editor.action.changeAll",
  "when": "editorTextFocus && !editorReadonly"
}

// You can view/customize VSCode keybindings by pressing F1 and typing Keybindings



2. 选择下一个匹配


您可以使用下面的键绑定选择下一个匹配。这有利于选择特定区域内的所有事件。

CTRL + d

默认Keybinding

{
  "key": "ctrl+d",
  "command": "editor.action.addSelectionToNextFindMatch",
  "when": "editorFocus"
}
 

边注

如果在按CTRL + D之前按下CTRL + K键绑定,它不会选择下一个实例,而是将您移动到下一个实例,并使用光标定位它。



默认Keybinding


3.重命名


这个还没有被提及,但除了在传统的IDE(如Visual Studio 2022或JetBrains: IntelliJ)中进行重构时,这个是你会用到的。

When you use this keybinding, it attempts to solve some of the problems that are incurred when using the other options listed above. The other options are overly greedy sometimes, and select parts of words that you didn't want to select, and if your not careful, you can delete quite a bit of code, resulting in messy situation. If you didn't notice that you screwed up right away, you end up saving, or working for a long while before having to reset everything, and you end up loosing a lot of work & time.

F2试图通过在底层实现逻辑来解决这个问题(IDK,如果它是一种算法或什么的),但它感觉非常像Visual Studio中的重构。它只选择您想要针对的特定情况。如果一个变量名为foo, is不会从名为fooFoo的变量中选择foo。它也不会从注释中选择foo。

F2

{
  "key": "f2",
  "command": "editor.action.rename",
  "when": "editorHasRenameProvider && editorTextFocus && !editorReadonly"
}



4. 找到


我将保持这个方法简短而巧妙,但是find将遍历您输入到编辑器小部件中的模式。

按CTRL + F,然后输入你想要选择的。一直按ENTER键直到找到它。




5. 最后最好的


没有一种方法可以每次都选择最好的,这就是为什么有几种不同的方法来做这件事,然而,有一些方法通常比其他方法更有用。据我所知,这个特性是VS Code独有的,我在处理大型JSON文件和大型代码库(如开源PR)时一直使用它。

VS Code has its own search editor, its a special editor. You can open the search editor by pressing on the Magnifying Glass Icon on the Activity-bar. Type into the search editors side bar text-input what it is you want to select, then press ENTER. It will return all the results in the sidebar. You can use the lower text input, to replace all of the results with what ever you like. You can also click OPEN IN THE EDITOR (it looks like a link) and it will reproduce everything you searched for in a new document, that is opened to the side. From there you can manipulate it, and add it back to the document. I've already written enough for one answer, so I am not going to go to deep into every thing it can do, but this not only selects everything, it also extracts it, replaces it, lets you nit-pick exactly what it means to select "all" of a specific occurrence. It's a great tool for making a single change in a recursive fashion.

其他回答

选择“查找匹配编辑器.action. selecthighlights”的所有事件。

Ctrl + Shift + L

Cmd+Shift+L或Cmd+Ctrl+G在Mac

根据Visual Studio Code的键绑定,有:

按Ctrl+Shift+L选择当前选择的所有事件

and

按Ctrl+F2选择当前单词的所有出现

你可以在命令面板(view ->命令面板)或键盘快捷键编辑器(File > Preferences > keyboard shortcuts)中查看VS Code中当前激活的键盘快捷键。

我在Windows 10中使用Ctrl + F2。

Ctrl + Shift + L启动性能日志记录

如果你只想选择几个呢?

没问题,首先:

Ctrl+F通过输入字母来查找字母 ESC退出搜索 (即使在使用时也需要这样做 Ctrl + Shift + L 选择所有事件)

OR

只需用鼠标或键盘选择这些字母 (Shift +箭头)

现在鼠标光标在第一个选择上闪烁, 使用更多的键绑定(感谢引用j08691) 你可以:

按Ctrl+D选择(添加)下一个事件 Ctrl + K + Ctrl + D跳过 当前/最新事件并添加下一个事件 Ctrl+U撤销最近的光标操作

已经列出了几个选项,但有两个没有。它可以使用重命名和重构工具,不只是选择所有,但做出特定的改变,在所有已经选择。我将尝试将所有我认为相关的答案捆绑在一起,并添加两个以上的答案,不仅可以完成工作,而且是在同一代码多次出现时进行单一更改的非常好的工具。



1. 选择所有匹配


若要使用多个游标来选择所有匹配项,可以使用2个键绑定中的1个。两个键绑定执行相同的功能,换句话说,2个键绑定,1个vscode命令。按键绑定如下。

1. CTRL + f2

2. CTRL + shift + l

默认Keybinding

{
  "key": "ctrl+f2",
  "command": "editor.action.changeAll",
  "when": "editorTextFocus && !editorReadonly"
}

// You can view/customize VSCode keybindings by pressing F1 and typing Keybindings



2. 选择下一个匹配


您可以使用下面的键绑定选择下一个匹配。这有利于选择特定区域内的所有事件。

CTRL + d

默认Keybinding

{
  "key": "ctrl+d",
  "command": "editor.action.addSelectionToNextFindMatch",
  "when": "editorFocus"
}
 

边注

如果在按CTRL + D之前按下CTRL + K键绑定,它不会选择下一个实例,而是将您移动到下一个实例,并使用光标定位它。



默认Keybinding


3.重命名


这个还没有被提及,但除了在传统的IDE(如Visual Studio 2022或JetBrains: IntelliJ)中进行重构时,这个是你会用到的。

When you use this keybinding, it attempts to solve some of the problems that are incurred when using the other options listed above. The other options are overly greedy sometimes, and select parts of words that you didn't want to select, and if your not careful, you can delete quite a bit of code, resulting in messy situation. If you didn't notice that you screwed up right away, you end up saving, or working for a long while before having to reset everything, and you end up loosing a lot of work & time.

F2试图通过在底层实现逻辑来解决这个问题(IDK,如果它是一种算法或什么的),但它感觉非常像Visual Studio中的重构。它只选择您想要针对的特定情况。如果一个变量名为foo, is不会从名为fooFoo的变量中选择foo。它也不会从注释中选择foo。

F2

{
  "key": "f2",
  "command": "editor.action.rename",
  "when": "editorHasRenameProvider && editorTextFocus && !editorReadonly"
}



4. 找到


我将保持这个方法简短而巧妙,但是find将遍历您输入到编辑器小部件中的模式。

按CTRL + F,然后输入你想要选择的。一直按ENTER键直到找到它。




5. 最后最好的


没有一种方法可以每次都选择最好的,这就是为什么有几种不同的方法来做这件事,然而,有一些方法通常比其他方法更有用。据我所知,这个特性是VS Code独有的,我在处理大型JSON文件和大型代码库(如开源PR)时一直使用它。

VS Code has its own search editor, its a special editor. You can open the search editor by pressing on the Magnifying Glass Icon on the Activity-bar. Type into the search editors side bar text-input what it is you want to select, then press ENTER. It will return all the results in the sidebar. You can use the lower text input, to replace all of the results with what ever you like. You can also click OPEN IN THE EDITOR (it looks like a link) and it will reproduce everything you searched for in a new document, that is opened to the side. From there you can manipulate it, and add it back to the document. I've already written enough for one answer, so I am not going to go to deep into every thing it can do, but this not only selects everything, it also extracts it, replaces it, lets you nit-pick exactly what it means to select "all" of a specific occurrence. It's a great tool for making a single change in a recursive fashion.