Search code examples
keyboard-shortcutsvisual-studio-codekey-bindings

Using ctrl+arrow to jump between punctuation in VSCode?


VSCode has a handy keybinding to jump by identifier using ctrl+arrow. However, I would like to change the behaviour of this slightly.

For example, my code looks like this: (| stands for the cursor)

|bar(foo.baz)

ctrl+right:

bar(|foo.baz)

I would like VSCode to treat the parentheses and other punctuation such as . as identifiers when I press ctrl+arrow so that each one has its own "stop". In my previous example I would like this to happen after repeated ctrl+right presses:

|bar(foo.baz)
bar|(foo.baz)
bar(|foo.baz)
bar(foo|.baz)
bar(foo.|baz)
bar(foo.baz|)
bar(foo.baz)|

This is what actually happens:

|bar(foo.baz)
bar(|foo.baz)
bar(foo.|baz)
bar(foo.baz)|

I've discovered that the Stack Exchange text editor has the behaviour I want, so you can try navigating some code in the Answer box using ctrl+arrow yourself to see what I mean.

Is there a setting or even extension that does this? I've tried googling but this problem is quite hard to describe in a short query.


Solution

  • You can use the extension Select By

    Define the following keybinding:

      {
        "key": "ctrl+right",  // or any other key combo
        "when": "editorTextFocus",
        "command": "moveby.regex",
        "args": {
          "regex": "(\\w+|.)",
          "properties": ["next", "end"]
        }
      }
    

    You can tweak the regex if you want different behavior.