10

After I search through files in the project directory with Command-Shift-f, the files that are opened are have the search string highlighted. The only way to unhighlight them seems to be to clear the search results. But typing "Esc" doesn't do that. I have to click the top-right corner "Clear Search Results" button. Is there a keyboard shortcut for that?

agentofuser
  • 8,987
  • 11
  • 54
  • 85

5 Answers5

9

Update:

Now you can add keybindings for these commands. Example:

{
    "key": "cmd+escape",
    "command": "search.action.clearSearchResults",
    "when": "editorTextFocus"
}

The best way to find these commands is by searching in the Keybindings editor. The others are search.action.refreshSearchResults and search.action.collapseSearchResults.


Original answer:

Not yet, but there is an open feature request to allow it: https://github.com/Microsoft/vscode/issues/23558

Rob Lourens
  • 15,081
  • 5
  • 76
  • 91
3

No, there's no default keyboard shortcut for "Clear Search Results."

E (among others) unhighlights search matches in the editor but doesn't clear search results.

To make Esc clear search results, add the key binding rule below to your keyboard shortcuts file.

{
    "key": "escape",
    "command": "search.action.clearSearchResults",
    "when": "hasSearchResult"
}

Instuctions: Key Bindings for Visual Studio Code: Advanced Customization

Side effect: Since this rule conflicts with Esc for closeFindWidget, you must use Esc to close the find widget when you also have search results. (There may be other conflicts. I don't know.)

Note: This command works when the editor has focus, but clearing search results (either with the mouse or keyboard) moves the focus to the search input box. You can use 1 to move the focus back to the editor.

ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • Annoying that focus is changing. Tried to use [macros](https://stackoverflow.com/a/49612562/3720305) but it doesn't work for this case. Anyway, thx – Jerry Green Feb 20 '19 at 12:59
0

Seems they added a Search: Clear command (which you will need to set your own shortcut for), but it only clears entries in the side panel, and only if you have actually run the search.

My workaround for this for a simple search is Ctrl+F Backspace Esc. That is, press hotkeys for a new search, which automatically highlights the old value, then delete that search and escape.

skylize
  • 1,401
  • 1
  • 9
  • 21
  • I'm using VS Code on macOS. I don't see a `Search: Clear` command. I see `Search: Clear Search History`, but that's different. Also, your workaround didn't work for me. – ma11hew28 Aug 19 '18 at 14:46
0

A long shortcut :-) is:

Select find-in-files string: ⇧⌘F

clear the string:

clear the results:

close the panel: ⌘J

(Mac → Windows Ctrl).

Denis Howe
  • 2,092
  • 1
  • 23
  • 25
0

This isn't precisely what the OP asked for but it is closely related. The idea is to fully undo the effects of pressing the shortcut for "Search: Find in Files", which means activate Explorer and focus the text editor.

{
    "key": "shift+escape",
    "command": "extension.multiCommand.execute",
    "args": {
        "sequence": [
            "workbench.view.explorer",
            "workbench.action.focusActiveEditorGroup"
        ]
    },
    "when": "searchViewletVisible"
}

This requires the multi-command extension.

Roman Starkov
  • 59,298
  • 38
  • 251
  • 324