In vim (and neovim), textobjects and motions are naturally linked, for instance in normal mode w
move to the next start of a word, while in operator pending mode w
denotes the word object, allowing viw
to select inside the word, and vaw
to select around the word.
Other builting objects include p
(a paragraph), {
(a matching pair of braces) or "
(a matching pair of quotes).
You can get more information on the builtin textobjects with:
|
|
Powerful textobjects
More powerful objects can be defined with the mini.ai plugin. This plugin allows using regexes, custom object definitions or even the syntax tree of your programming language to define smart objects. For instance, the following line uses the syntax tree to define a function definition object:
|
|
This code block defines a new textobject f
that selects a function definition: vaf
to select around it, and vif
to select inside it.
Powerful motions
As of today, mini.ai does not provide a configuration option to define powerful motions to jump to the next/previous text object like w
in normal mode would. But we can define these motions ourselves.
Mini.ai does expose a lua function to move the cursor the the next/last start/end of a textobject:
|
|
We can leverage this function to define a motion grammar:
Let’s say for instance that we repurpose the keys n
and p
as our main motion operators:
n
: go to next startp
: go to prev startne
: go to next endpe
: go to prev endni
: go to next start (inside)pi
: go to prev start (inside)
First we retain the original behavior of n
and p
as follows:
nm
: go to next search matchpm
: go to prev search match
We can define the following mappins to move to the next/prev start/end of a function:
|
|
I have opened a feature request to merge these motions inside mini.ai.