Custom shortcut to find current word in all files

92 Views Asked by At

I am trying to define a shortcut to find the word under the cursor in all files in VSCode with Vim extension enabled.

Usually I would do:

  1. yiw (to copy the word under the cursor)
  2. Ctrl+Shift+F (to open the find in all files panel)
  3. Ctrl+V (to paste the copied word)

The idea would be to execute the steps by pressing my leader key (space) and F, but I could live with a Ctrl+F10 or similar solution.

What's the best approach here? Could this be done just with Vim?

I tried stuff like:

  "vim.normalModeKeyBindings": [{
    "before": ["<leader>", "f"],
    "after": [ "y", "i", "w", "workbench.action.findInFiles", "<C-v>" ]
  }]

but it doesn't accept commands like that, only keystrokes (?).

After some initial tests, I'm guessing I need the Multi-Command Extension to be able to replicate all the steps (I'm not sure, though).

I also tried:

"key": "ctrl+f10",
"command": "extension.multiCommand.execute",
"args": {
  "sequence": [
    "editor.action.addSelectionToNextFindMatch", // trying to select the word here, it's incomplete.
    "workbench.action.findInFiles",
    "workbench.action.paste"
  ]
}

but it doesn't respond, maybe Vim mode blocks it in some way? Keybindings.json doesn't have a ctrl+f10 keybinding, so it should be free.


EDIT: Just for clarification, I finally added this to "vim.normalModeKeyBindings" in settings.json to make it work:

  "before": ["<leader>", "f"],
  "commands": [
    "editor.action.addSelectionToNextFindMatch",
    {
      "command": "workbench.action.findInFiles",
      "args": {
        "query": "${selectedText}",
        "triggerSearch": true
        // "isRegex": true,
        // "replaceString": "******",  
      }
    }
  ],

Thanks to Mark's reply.

1

There are 1 best solutions below

2
Mark On BEST ANSWER

You can do this easier than you think. Try this keybinding:

 {
    "key": "ctrl+shift+f",                // whatever keybinding you want
    "command": "workbench.action.findInFiles",
    "args": {
      "query": "${selectedText}",          // uses the selected text
      // "replace": " **** ",
      // "isRegex": true,
      "triggerSearch": true,          // seems to be the default
      // "filesToInclude": "src, include",
      // "filesToExclude": "data",
      // "preserveCase": true,
      // "useExcludeSettingsAndIgnoreFiles": false,
      // "isCaseSensitive": true,
      // "matchWholeWord": true,
    }
  }

or, to automate the selection:

{
  "command": "runCommands",
  "key": "alt+r",
  "args": {
    "commands": [

      "editor.action.addSelectionToNextFindMatch",

      {
        "command": "workbench.action.findInFiles",
        "args": {
          "query": "${selectedText}",
          "triggerSearch": true
          // "isRegex": true,
          // "replaceString": "******",  
        }
      }
    ]
  }
}

runCommands is a built-in vscode command that can run multiple commands, so no need for multiCommand here