visual-studio-code

How to sync search term between tab groups?


It's common for me to have two tab groups open to compare things side-by-side but when I am searching for a term I find it a bit annoying that the search term isn't shared by the two editors. e.g. I ctrl-F3 on a word in the file to the left to start searching, then move over to the file on the right and I'd like to just hit F3 in order to continue searching for the same word, but it has it's own term saved to search for.

I assumed that this would be an option somewhere, but I don't see it in the Text Editor -> Find section of the pref's. Am I just blind and not seeing it or does this option not exist in VS Code?


Solution

  • Update: see https://stackoverflow.com/a/65851872/836330 "Searching only in open editors" might work for you.


    I don't think there is an option to tie the find's together between groups like that. But with a macro you could do this fairly easily. Using a macro extension like multi-command put this into your settings:

      "multiCommand.commands": [
    
        {
          "command": "multiCommand.findAcrossGroups",
          "sequence": [
            "editor.action.addSelectionToNextFindMatch",
            "editor.action.clipboardCopyAction",
            "workbench.action.focusNextGroup",
            "actions.find",
            "editor.action.clipboardPasteAction",
            "editor.action.nextSelectionMatchFindAction",
            "workbench.action.focusPreviousGroup"   // to return to first group
          ]
         }
        ]
    

    and a keybinding to trigger it (in keybindings.json):

      {
        "key": "alt+s",    // whatever binding you wish
        "command": "extension.multiCommand.execute",
        "args": { "command": "multiCommand.findAcrossGroups" },
        "when": "editorTextFocus"
      }
    

    How you would use this is when you trigger the keybinding, whatever word is under your cursor in group 1 will be transferred to the find widget in group 2 (and focus will return to group 1, don't use the last command "workbench.action.focusPreviousGroup" if you want to use this as you move to the next group. And then F3 works as you expect when you switch to group 2.

    One downside to this approach is that it needs to use your clipboard to transfer the word under the cursor, that may not be an issue for you.

    I made this to work with two groups primarily, although it really uses next/previous groups so it also works for any two adjoining group. It could be generalizeed to three groups pretty easily - beyond that it would probably be easier to make an extension to work with as many groups as you have open.

    find across groups