问题
In Visual Studio, when working with C#, I can click on a variable, hold down CTRL
+ SHIFT
and move between it's references using the arrow keys (up and down) inside the script that's currently open.
How can I perform such an action in VS Code while working with TypeScript?
回答1:
I wouldn't call them references since that terminology is usually used for other things in the editor, you can use any of the following:
Scopeless
editor.action.nextMatchFindAction
Default F3
editor.action.previousMatchFindAction
Default SHIFT + F3
If the findInputFocussed
criteria is met (meaning you have actively focused on the find
input (CTRL + F) you can simply use Enter to go to the next match and SHIFT + Enter for previous or you can continue to use the same F3 bindings; they work harmoniously.
You could also just remap them to what you are using, don't have to keep it F3
A caveat to this approach is that your find input value must be cleared for this to work if your word is not selected and there is already an existing query that is not the same as the word. However, if your word is selected it will automatically put the word into the find input box - which is effectively the same thing as pressing CTRL + F when your cursor is in a word
Scope based
Similar to above, without the find/replace widget and not as many constraints, but it is limited to a 'region'.
This method cycles indefinitely and is case sensitive.
editor.action.wordHighlight.next
Default F7
editor.action.wordHighlight.prev
Default SHIFT + F7
Specifically for a language
You could override these to use CTRL+SHIFT+Navigation as you are used to if you wish, in addition to specifying the language id in the when expression
to only work in typescript, though, it might be good to use the same keybindings across your workspaces for continuity since these keybinds are the same for other scenarios like when you are in the terminal, dealers choice though; too each their own.
Here's an example shortcut setting (you will have to put in your keybindings.json
if you want to make a duplicate):
{
"key": "ctrl+shift+down",
"command": "editor.action.nextMatchFindAction",
"when": "editorTextFocus && editorLangId == 'typescript'"
}
Using symbols
Typescript supports symbols, so if you want to search based on scope you can use:
workbench.action.gotoSymbol
Defaults Ctrl+Shift+O
Another related way is to add the next find match to a selection using CTRL + D which adds the next found match and selects it for editing (think multiple cursors)
commandId: editor.action.addSelectionToNextFindMatch
来源:https://stackoverflow.com/questions/63013392/how-to-move-to-the-next-previous-variable-reference-in-vs-code