Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quickly search for a symbol under cursor in all project files in Visual Studio Code

While browsing through C++ code in VS Code, I am looking for a better way (possibly just a simple keyboard shortcut after some configuration) to do the following:

  1. My cursor is hovering over a symbol (e.g. MyClass class).
  2. I want to search for this symbol in all project files (i.e. perform Ctrl + Shift + F search on the symbol under cursor)

At present I don't know a better way then to copy the symbol name -> Ctrl + Shift + F -> paste the name -> hit 'Enter'.

like image 743
tkleczek Avatar asked Nov 28 '17 15:11

tkleczek


2 Answers

As of version 1.42.0 there is no single-keystroke way of doing it (although I would appreciate this very much).

You could use the sequence Ctrl-D, Ctrl-Shift-F.
("Add Selection To Next Find Match")

Better: There is an extension exactly for this use case, it's called:
"Search Under Cursor"

like image 86
der Vodi Avatar answered Oct 19 '22 05:10

der Vodi


I added a single keychord method to my extension: Find and Transform. So if you set up a keybinding like:

{
    "key": "alt+z",      // whatever keybinding you like
    "command": "runInSearchPanel"
}

you can trigger the keychord when the cursor is on (or immediately after) a word or select a word and the Search Panel will open with that word as the query.

The command can also take a lot of arguments, like:

{
    "key": "alt+z",
    "command": "runInSearchPanel",
    "args": {
        "find": "(?<=^Arturo)\\d+",   // fixed-width lookbehinds and multiline supported
        "replace": "###",
        "matchWholeWord": false,
        "isRegex": true,
        "filesToInclude": "${file}",  // resolves to current file
        "triggerSearch": true
    }
}

and other args if you want to save a pre-defined search as a keybinding or as a setting that will be exposed in the Command Palette. More at the README.


Previous Answer:

You don't have to copy or paste the symbol, just highlight all of it. You can do that in various ways such as double-clicking the word or I like the vscode expand region extension. Then Ctrl-Shift-F and Enter to perform the search.

This is assuming you have

// Controls if we seed the search string in Find Widget from editor selection

"editor.find.seedSearchStringFromSelection": true,

in your settings. true is the default.

[ Apologies to @yorammi if this is what you said, I couldn't follow your instructions ... ]

like image 27
Mark Avatar answered Oct 19 '22 03:10

Mark