6

There has been quite a bit of discussion (1, 2, 3) about whether "find" in vscode should default to the whole file, or to the selection. The discussion seems to have converged toward disabling "in selection" by default, but providing the configuration option "editor.find.autoFindInSelection" to change this default (with three options never, always, only on multiline selection).

However, I find that in my workflow I never want the "in selection" active when I do a simple find, but I do want it when I do a find/replace with a multiline selection.

Is there a setting to only toggle "in selection" when I invoke find/replace (Ctrl+H for me), not for a simple find (Ctrl+F for me)?

user313032
  • 604
  • 1
  • 7
  • 18
  • 1
    Was just searching for this option and the "multi-line" selection should be sufficient at least for me. It is pretty rare for me to do a find and replace with multiple lines selected. – Ahmed May 08 '22 at 05:37

1 Answers1

3

Is there a setting to only toggle "in selection" when I invoke find/replace (Ctrl+H for me), not for a simple find (Ctrl+F for me)?

You could set up the following keybindings using the new command editor.actions.findWithArgs:

{
  "key": "ctrl+f",
  "command": "editor.actions.findWithArgs",
  "args": {
    "findInSelection": false,
    // "isRegex": false,
  }
},
{
  "key": "ctrl+h",
  "command": "editor.actions.findWithArgs",
  "args": {
    "findInSelection": true,
    // "isRegex": true,
    "replaceString": "",
  }
}

These mostly work. I have filed a bug: https://github.com/microsoft/vscode/issues/141683

If the Find Widget is closed, these do work as expected. Although you will only get the Find In Selection option enabled if you have a selection when you trigger Ctrl+H. I think that requirement is okay.

What doesn't work and is the subject of the issue is if the Find Widget is already open and you Ctrl+H, the Find In Selection option is not disabled. It should be - the isRegex option is disabled in this case as per the above keybindings.

So almost there if the issue is resolved.

user313032
  • 604
  • 1
  • 7
  • 18
Mark
  • 143,421
  • 24
  • 428
  • 436
  • I just un-accepted the answer. I don't know if it used to do what I want and a recent vscode update broke it, or if it never worked as I wanted and I didn't notice it. In any case, I want Ctrl+h to act on the whole file if there is no active selection, and now it toggles the "in selection" even if there is no selection active. – user313032 Dec 20 '20 at 05:31
  • This did not work in my case. – abunickabhi Jan 17 '22 at 11:23
  • @abunickabhi I changed the answer to use a new command that works much better. – Mark Jan 28 '22 at 23:22
  • I pasted your suggestion in keybindings.json, but with that both shotcuts only gave me the one-line search field without the replace field. Then I tried replacing "editor.actions.findWithArgs" in the ctrl+h shortcut with "editor.action.startFindReplaceAction", but after that it wouldn't activate the "in selection" anymore. – user313032 May 09 '22 at 23:26
  • OK I came back to this after quite a while, and saw that the proposed solution didn't work because of a single mistake. I fixed that now and accepted it. – user313032 Jul 23 '23 at 04:36