Firehawke
Firehawke

Reputation: 13

Possible to move cursor to next instance of specific word?

I need to run VSCode through some XML that's at least 90% auto-generated. There are a few things the automatic generation can't determine on its own and needs manual intervention on-- hand-editing where a specific piece of text will be generated on each entry.

What I'm trying to do is to find a way (through extension or through built-in commands) to set up a keybinding that will move the cursor down to the next instance of a specific phrase of my choice (that I wouldn't be manually typing; it would be assigned as part of the keybind) in the current file when I press the binding for it.

For instance, press F17 to go to the next instance of "FIXME" in the current document.

I've been through at least a dozen extensions and haven't found anything that matches my need, and the built-in actions.find doesn't support parameters. actions.findWithSelection requires the text to be selected, so that won't work either.

Any ideas? Someone surely has had the same need for this at some point. I can't see this as a rare need at the very least.

Upvotes: 1

Views: 486

Answers (2)

Mark
Mark

Reputation: 181050

I modified the other extension I mentioned, Find and Transform, to make this really easy. With this keybinding

{
  "key": "alt+r",
  "command": "findInCurrentFile",
  "args": {
    "find": "FIXME",
    // "replace": "DONE",
    "restrictFind": "nextMoveCursor"
  }
}

or


{
  "key": "alt+r",
  "command": "findInCurrentFile",
  "args": {
    // "find": "FIXME",        // no find necessary !!
    // "replace": "DONE",
    "restrictFind": "nextMoveCursor"
  }
},

The first example uses a fixed find and will go to the next FIXME from wherever the cursor is - and it will wrap to the beginning of the file if there are no matches in the rest of the file.

The second example will use the word under the cursor, and thus requires that you start at the word FIXME for example.

First demo (with a find value):

move Cursor to next find

Second demo (with no find, note using during words under cursor to search):

move cursor to next without find

The extension can do a lot more. There are many examples at its link above.

{
  "key": "alt+r",
  "command": "findInCurrentFile",
  "args": {
    "find": "FIXME",
    // "replace": "DONE",
    "restrictFind": "nextSelect"
  }
}

The nextSelect option will go to and select the matches in turn so you could modify some of the selections and then move to the next.

select next occurrence



I think this extension (that I wrote) is what you are looking for: Jump and select.

Example keybinding (without optional selection of the phrase):

{
    "key": "alt+r",                       // whatever keybinding you wish
    "command": "jump-and-select.jumpForward",
    "args": {
            "text": "FIXME",
            // "restrictSearch": "document",
    }
},

find extension demo

[If you actually wanted to find all matches and optionally replace them with a pre-defined regex find/replace see Find and Transform.]

Upvotes: 1

myf
myf

Reputation: 11283

You are most probably looking for Move Last Selection To Next Find Match (editor.action.moveSelectionToNextFindMatch), by default bound to ctrl+k ctrl+d. Opposite direction (editor.action.moveSelectionToPreviousFindMatch) has no default binding.

Personally I use Ctrl+Shift+vertical arrows for this, and take my word I use them all the time. keybindings.json snippet:

  {
    "command": "editor.action.moveSelectionToPreviousFindMatch",
    "key": "ctrl+shift+up",
    "when": "editorFocus"
  },
  {
    "command": "editor.action.moveSelectionToNextFindMatch",
    "key": "ctrl+shift+down",
    "when": "editorFocus"
  },

Indeed, these command names are quite impossible to find in command palette, when you just want to "jump to next occurrence of word or selection under cursor". It is beyond comprehension how they got such convoluted names.


Congratulations for the first question, BTW!

Upvotes: 0

Related Questions