Skip to main content

Use this tag for questions about creating your own operator within vim.

Vim allows you to create custom operators. From :help :map-operator

1.11 MAPPING AN OPERATOR                *:map-operator*

An operator is used before a {motion} command.  To define your own operator
you must create mapping that first sets the 'operatorfunc' option and then
invoke the |g@| operator.  After the user types the {motion} command the
specified function will be called.

                            *g@* *E774* *E775*
g@{motion}      Call the function set by the 'operatorfunc' option.
            The '[ mark is positioned at the start of the text
            moved over by {motion}, the '] mark on the last
            character of the text.
            The function is called with one String argument:
                "line"  {motion} was |linewise|
                "char"  {motion} was |characterwise|
                "block" {motion} was |blockwise-visual|
            Although "block" would rarely appear, since it can
            only result from Visual mode where "g@" is not useful.
            {not available when compiled without the |+eval|

The basic usage is to define a function, and the map your desired keystroke to

:set opfunc=MyCustomFunc<cr>g@

The g@ operator will call whatever function you have bound to opfunc, and set some marks to allow you to define how your operator should behave.