简体   繁体   中英

replace in selection in vscode

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)?

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)?

Yes, this can be done. You also talked about a multiline selection which would be much harder to detect (as opposed to a singeline/word selection) and probably require an extension.

But to always have Ctrl + F (Find) never have the Find in Selection enabled and to always have it enabled with Ctrl + H (Replace) you can do this:

  1. Editor > Find: Auto Find in Selection set to never

  2. You will need a macro extension, here using Multi-Command , put this into your settings:

     "multiCommand.commands": [ { "command": "multiCommand.replaceInSelection", "sequence": [ "editor.action.startFindReplaceAction", // bring up the Find/Replace widget "toggleFindInSelection" // toggle off the Find in Slection option ] } ]

and a keybinding (in keybindings.json):

{
  "key": "ctrl+h",
  "command": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.replaceInSelection" },
  "when": "editorFocus || editorIsOpen"
}

This keybinding will trigger the macro. The macro will toggle the Find in Selection option to on whenever you hit Ctrl + H .

Ctrl + F (Find) will be unaffected and since Auto Find in Selection in your settings was set to never the option will not be enabled with a simple Find.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM