11

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?

5 Answers 5

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

2
  • That issue was closed on Dec 4, 2017, and it doesn't answer the question.
    – ma11hew28
    Commented Aug 19, 2018 at 13:27
  • It added the requested feature, I'll update the answer. Commented Aug 20, 2018 at 5:04
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.

1
  • Annoying that focus is changing. Tried to use macros but it doesn't work for this case. Anyway, thx Commented Feb 20, 2019 at 12:59
1

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.

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.

1
  • 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
    Commented Aug 19, 2018 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).

Not the answer you're looking for? Browse other questions tagged or ask your own question.