Is it possible? Absolutely! Case and point: textobj-word-column.vim.
How to make your own text objects
Typically visual mode is used to create a new text object. The visual mode can be line-wise, character-wise (typically), or visual-block. Here is the basics of what you will need:
- An unused key combination typically
a{char}
or i{char}
where {char}
is both descriptive and unused. e.g. i/
as an example text object between /
's.
- Need a way to find the start of your text-object. e.g.
T/
- Need a way to find the end of your text-object. e.g.
t/
- Choose a visual mode. e.g.
v
- Create a visual mode (only) mapping via
xnoremap
.
- Create a operator pending mode mapping that uses the visual mode mapping via
onoremap
and :normal
.
Now for an example of our simple i/
which creates a text-object between /
's:
xnoremap i/ :<c-u>normal! T/vt/<cr>
onoremap i/ :normal vi/<cr>
As long as you follow the basic ingredients you can create text-objects for all sorts of things.
Advanced text-object creation with vim-textobj-user
The vim-textobj-user plugin provides a common way to define custom text-objects in a more declarative fashion. For example here is a php tag text-object:
call textobj#user#plugin('php', {
\ 'code': {
\ 'pattern': ['<?php\>', '?>'],
\ 'select-a': 'aP',
\ 'select-i': 'iP',
\ },
\ })
For more help with vim-textobj-user see its help doc: :h textobj-user-introduction
.
For more help
:h map-overview
:h visual-start
:h :norm
As well as the Creating new text objects Vim wiki page.