Selection Motions

There are two advantages to these motions over the built-in motions

Below are the pre-defined motions, all of which can be customized. For ultimate flexiblity, you can use the generic moveby command. The units are defined by regex's (listed in the next section).

Move cursor

These commands move the cursor, without selecting.

Move selection

These commands adjust both the start and end of the selection, so that it surrounds the given unit.

Select to unit

These commands adjust one end of the selection by the given unit.

Custom Motions

You can define any units you wish to move the cursor by using motionUnits.

These are defined in your settings file (open with the command Preferences: Open Settings (JSON)), using selection-utilities.motionUnits. This setting is an array with entries containing a name and a regex value. Each regex is compiled with the g and u flags (global search and unicode support). The default units are listed below.


"selection-utilities-motionUnits": [
    { "name": "WORD", "regex": "[^\\s]+" },
    { "name": "word", "regex": "(_*[\\p{L}][_\\p{L}0-9]*)|(_+)|([0-9][0-9.]*)|((?<=[\\s\\r\\n])[^\\p{L}^\\s]+(?=[\\s\\r\\n]))" },
    { "name": "subword", "regex": "(_*[\\p{L}][0-9\\p{Ll}]+_*)|(_+)|(\\p{Lu}[\\p{Lu}0-9]+_*(?!\\p{Ll}))|(\\p{L})|([^\\p{L}^\\s^0-9])|([0-9][0-9.]*)" },
    { "name": "paragraph", "regexs": "\\S+" },
    { "name": "section", "regexs": [ ".+", "^.*========+.*$" ] },
    { "name": "subsection", "regexs": [ ".+", "^.*(========+|--------+).*$" ] }
],

Motion units can be customized on a per-langauge basis, if desired.

For the built-in motions to work, all of the above units have to be defined, but you can always add more units types if you want to use the moveBy command (describe below).

Note that some of the units employ multi-line matches using regexs instead of regex. For more on how to define multi-line units, see the final subsection below.

The custom moveBy command

The moveby command moves the cursor according to one of the regular expressions you defined in your settings. It takes five optional arguments.

For example to move the cursor to the start of the next number, (using the regex definitions provided above), using ctrl+# you could define the following command in your keybindings.json file.

{
    "command": "vscode-custom-word-motions.moveby",
    "args": { "unit": "number" },
    "key": "ctrl+shift+3"
}

The narrowTo command

The narrowto command shrinks the current boundaries of the current selection until it is directly at the given boundaries of the regular expression. It takes four optional arguments.

For example, to narrow the boundaries of the selection to lie at non-white-space characters by pressing cmd+( you could add the following to keybindings.json.

{
    "command": "vscode-custom-word-motions.narrowto",
    "args": { "unit": "WORD" },
    "key": "shift+cmd+9",
}

Multi-line units

The units can work for multi-line matches. To use this feature, change the unit entry to use regexs instead of regex.

If a single regular expression is provided, the match will occur to a contiguous series of lines which all match that expression. If multiple expressions are provided, the match will occur to a sequence of lines that match those expressions.

For instance, to select a group of contiguous non-whitespace lines with "cmd+shift+[" you could add the following definition to preferences.

"vscode-custom-word-motions.units": [
    {"name": "paragraph", "regexs": "\\S+"},
]

Then add the following definition to keybindings.json

{
    "command": "vscode-custom-word-motions.moveby",
    "args": { "unit": "paragraph", "selectWhole": true },
    "key": "shift+cmd+9",
}

Or, for instance, you could select sections of code separated by comment headers

"vscode-custom-word-motions.units": [
    {"name": "section", "regexs": [".+", "^.*------.*$"]}
]"

Where a section header looks like this

// My section
// -----------------------------------------------------------------

You could then select all code in the section using a keybinding like follows.

{
    "command": "vscode-custom-word-motions.moveby",
    "args": { "unit": "section", "boundary": "start", "selectWhole": true },
    "key": "shift+cmd+0",
}
View on GitHub