Skip to content

5. Operators

mg979 edited this page May 24, 2019 · 5 revisions

Find Operator

Its purpose is to find all matches inside a visual selection, or a text object. To start it from normal mode:

  • m + text object ('m' stands for 'matches')

You can also start it from visual mode:

  • leader-f (search VM patterns or current search register)
  • leader-/ (start regex search inside visual selection)

If run from visual mode with leader-f, and VM hasn't started yet, it will look for patterns matching the current search register. Otherwise it will look for the current patterns. Method 3. can only work after VM has started.

Example from visual mode, using vim-indent-object: press C-n, then vii and leader-f to select all occurrences inside the indentation level.

Imgur

Example from normal mode, using vim-textobj-function: press C-n, then maf to select all occurrences inside the function.

Imgur


Select Operator

The Select Operator is activated with leader-gs or s (after VM has started). leader-gs creates selections from nothing, while s creates them from existing regions.

They're different because:

  • leader-gs always creates a new region, and never touches existing regions.
  • s acts on all regions at the same time, and will remove the regions/cursors that have been used as 'jump-points'.

That is, s creates regions from cursors: from a cursor, a new region is made, but the old cursor is removed.

You can use the Select Operator in the same way you would use the v key in vim. While v starts Visual Mode and creates a visual selection, the Select Operator starts extend mode (if it's not started already) and creates selections for every region, similarly to how v would create them.

Imgur

leader-gs s
VM not started? creates selection mapping not set
cursor mode? creates selection and turns on extend mode removes cursors and creates selections out of them
extend mode? creates selection removes selections and creates new selections out of them

The Select Operator is basically a yank motion, so it supports all custom operators from plugins you may have installed, eg:

wellle/targets
kana/vim-textobj-indent
kana/vim-textobj-function


Cursor Operators

In cursor mode, several operations are allowed, with some limited plugin support. All default yank, delete and change operations are supported, with registers and count, for example "zdw, c2b, etc.

vim-surround is explicitely supported, while others are implicitely supported if they replace default vim motions/text objects.

operator special example
y s,S ysiW]
d s ds(
c s,S cs(]

kana/vim-textobj-function and kana/vim-textobj-indent are also supported, so that change operations will work correctly. An example using cii and kana/vim-textobj-indent:

Imgur


Smart case change

This is alternative to the typical change operator, and is run with gc, rather than c. In this mode, at each cursor, the inserted text will be capitalized or uppercased, if the replaced text was.

Imgur

Moreover, for more complex transformations of the inserted text, one may run the command VMSmartChange that accepts as argument the name of a function, eg. MyTextTransform(cursor, text):

:VMSmartChange MyTextTransform

fun! MyTextTransform(cursor, txt) abort
    " the active cursor isn't affected, text is entered as typed
    try
        let original_text = b:VM_Selection.Vars.changed_text[a:cursor.index]
        if match(original_text, '\u') >= 0 && match(original_text, '\U') < 0
            return toupper(a:txt)
        elseif match(original_text, '\u') == 0
            return toupper(a:txt[:0]) . a:txt[1:]
        else
            return a:txt
        endif
    catch
        return a:txt
    endtry
endfun

Currently this command can only be run inside VM, and must be run just before the text change (this should be improved in the future).