mauroformigoni
mauroformigoni

Reputation: 139

How to do a search for the selected text?

How do I search for the selected text in the current file without having to copy / ctrl-f / paste?

For clarification: Ultraedit has this behaviour. When pressed F3 and there's no selected text it performs the last search, if there is a selected text then it searches for the selected text in the current file.

Upvotes: 7

Views: 6076

Answers (3)

Jackie Yeh
Jackie Yeh

Reputation: 1627

Ultraedit-way search is my favorite and it's really convenient: single 'F3' key can handle all.

The drawback of Ctrl+D is: it cannot wrap around the search.

To be clear, the definition of Ultraedit-way search is: When pressed F3 and there's no selected text it performs the last search, if there is a selected text then it searches for the selected text in the current file.

Here is the absolutely 100%-compatible solution to Ultraedit-way search:

  • When text is selected, do nextSelectionMatchFindAction
  • When no text is selected, do nextMatchFindAction
  • Single F3 does both above.
  • Shift+F3 keeps its original: Find Previous

Therefore keybindings.json can add the following lines to disable original F3 and Ctrl+F3 functions, and add two new F3 functions when text is selected and not selected.

{
    "key": "f3",
    "command": "editor.action.nextSelectionMatchFindAction",
    "when": "editorFocus && editorHasSelection"
},
{
    "key": "ctrl+f3",
    "command": "-editor.action.nextSelectionMatchFindAction",
    "when": "editorFocus"
},
{
    "key": "f3",
    "command": "editor.action.nextMatchFindAction",
    "when": "editorFocus && !editorHasSelection"
},
{
    "key": "f3",
    "command": "-editor.action.nextMatchFindAction",
    "when": "editorFocus"
}

And one more thing to be addressed: When F3 is pressed, the search dialog is appeared and every matched text is highlighted, you can press ESC to dismiss search dialog when search is done.

Update @2021/1/25 If anyone wants Shift+F3 to works as smart as F3, add the following line into keybindings.json:

{
    "key": "shift+f3",
    "command": "editor.action.previousSelectionMatchFindAction",
    "when": "editorFocus && editorHasSelection"
},
{
    "key": "shift+f3",
    "command": "editor.action.previousMatchFindAction",
    "when": "editorFocus && !editorHasSelection"
},
{
    "key": "shift+f3",
    "command": "-editor.action.previousMatchFindAction",
    "when": "editorFocus"
},

Upvotes: 4

mauroformigoni
mauroformigoni

Reputation: 139

I have found what I was looking for.

Ctrl+D triggers the action Add Selection To Next Find Match which does exactly what I wanted: perform an immediate search for the selected text, just like F3 in Ultraedit.

Upvotes: 1

Alex Myers
Alex Myers

Reputation: 7155

Enable the editor.find.seedSearchStringFromSelection setting as seen below. This will cause highlighted texted to automatically be searched when you press ctrl+f.

enter image description here

Upvotes: 7

Related Questions