Edit: I just discovered the vim-textobject-latex plugin, which adds the exact functionality you're looking for (in addition to a few other things). From the plugin's README:
Currently supported text objects are:
a i Description
a\\ i\\ Inline math surrounded by ``\\(`` and ``\\)``.
a$ i$ Inline math surrounded by dollar signs.
aq iq Single-quoted text ``\`like this'``.
aQ iQ Double-quoted text ``\``like this''``.
ae ie Environment ``\begin{...}–\end{...}``
So you can use caq
and the like as you wanted. I tested it, and it works as advertised, but note that it is dependent on the vim-textobject-user plugin. Also note you must make sure that the textobject-user
plugin loads first, otherwise things won't work.
If you don't want to mess with plugins, however, my original answer remains useful:
Adding these two lines to your .vimrc
will cover most of what you want.
onoremap aq :<c-u>normal! F`vf'<cr>
onoremap iq :<c-u>normal! T`vt'<cr>
These create new text objects (aq
and iq
) which go back to the previous `
, enter visual selection mode, then select up to the next '
.
This will work for ciq
, caq
, yiq
, and diq
. However, yaq
and daq
will not yank/delete the outer quotes, so I created another text object, a"
, to handle that (i"
is not needed, but I added to keep things consistent:
onoremap a" :<c-u>normal! 2F`v2f'<cr>
onoremap i" :<c-u>normal! 2T`v2t'<cr>
The major downside here is that this will break normal ca"
, etc. use. If you preface them with au FileType tex
, you'll only have that issue in .tex
files, but it still might occasionally be an annoyance. You could also name the object something like ad
/id
('d' for 'double' quote), but that seemed less intuitive to me (I was aiming for the least difference from normal operation).
The only way I can think of to wrap these up into just two text objects would be to write a function that checks for another quote outside of the first set and set the aq
text objects to call that.
Note: There are a couple other minor issues with these:
- Yanking might leave your cursor in an unexpected place (this could be fixed by adding using a mark in the commands (e.g.
ma2F`v2f'<cr>`a
), just make sure not to use one you frequently use manually)
- The cursor must be inside the quotes; weird things happen if it is on the quotes.
To wrap all of that up, this is what I personally would add to my .vimrc
for this issue:
augroup filetype_latex
au!
au FileType tex,plaintex onoremap a' :<c-u>normal! muF`vf'<cr>`u
au FileType tex,plaintex onoremap i' :<c-u>normal! muT`vt'<cr>`u
au FileType tex,plaintex onoremap a" :<c-u>normal! mu2F`v2f'<cr>`u
au FileType tex,plaintex onoremap i" :<c-u>normal! mu2T`v2t'<cr>`u
end augroup
As you can see, I opted for breaking normal ca'
/ya"
/etc functionality so I wouldn't have to learn new commands for them since I wouldn't be using the normal functionality as much within LaTeX anyways. That's just a personal preference; not hard to switch the object names. I also added in the filetype detection and marks.