There are following text objects at the moment:
- ic ac Commands
- id ad Delimiters
- ie ae LaTeX environments
- i$ a$ Inline math structures
I propose to add one more:
- is as Sections (Subsections, Subsubsections, etc)
Please provide a minimal working LaTeX example, e.g.
\documentclass{minimal}
\begin{document}
Hello World!
\section
La-la-la
\subsection
----- Begin of subsection
some text
----> CURSOR POSITION
\subsubsection
----- End of subsection
\subsection
La-la-la
\end{document}
I want vis to select text from ----- Begin of subsection to ----- End of subsection.
It is usually very useful text object (for example when you want to change notion inside one section)
Since vimtex automatically defines folds for this, you can use https://github.com/kana/vim-textobj-fold and the textobjects iz or az.
Update They might not be perfect because they do not allow to extend the selection by pressing again iz to select the next fold level. For comparison consider following hello world html code
<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML>
<HEAD>
<TITLE>
A Small Hello
</TITLE>
</HEAD>
<BODY>
<H1>Hi</H1>
<P>This is very minimal "hello world" HTML document.</P>
</BODY>
</HTML>
Place your cursor on A Small Hello and press vat. The visual selction will be
<TITLE>
A Small Hello
</TITLE>
When you now press at without leaving visual mode, the selection will extend to
<HEAD>
<TITLE>
A Small Hello
</TITLE>
</HEAD>
This limitation of textobj-user based textobjects is documented here:
Please note, is and as are builtin textobjects for sentences (:h is). I would not advice to override them, even though they might not be used very often.
First, I think is and as would be unsuitable, both because they are already text objects for sentences, and because they are suggested text objects for vim-sandwich.
Further, I think @kiryph has a good suggestion, that you can use vim-textobj-fold to get the desired text objects.
@lervag I just realized that vimtex ae cannot be extended as I have described in my previous comment for the html tag textobject at. Would you consider improving your textobjects to support this?
I add an example to illustrate this
\begin{itemize}
\item
\begin{itemize}
\item CURSOR
\end{itemize}
\item
\end{itemize}
Pressing vae selects

When I now press ae, the visual area does not select everything which would be nice to have.
I think the same idea could be applied to nested commands and delimiters. Since inline math environments won't be nested, I guess it is not useful for this.
Vim textobject plugins which support extending are
UPDATE
sandwich includes since February 2017 predefined rules for tex. Using vabab will actually select the surrounding environment when the cursor is within the nested environment.
Defining textobjects with the help of sandwich one get this extendable behaviour.
Add following lines to .vim/after/ftplugin/tex.vim
omap <buffer> ie <Plug>(textobj-sandwich-query-i)e
xmap <buffer> ie <Plug>(textobj-sandwich-query-i)e
omap <buffer> ae <Plug>(textobj-sandwich-query-a)e
xmap <buffer> ae <Plug>(textobj-sandwich-query-a)e
omap <buffer> ic <Plug>(textobj-sandwich-query-i)c
xmap <buffer> ic <Plug>(textobj-sandwich-query-i)c
omap <buffer> ac <Plug>(textobj-sandwich-query-a)c
xmap <buffer> ac <Plug>(textobj-sandwich-query-a)c
omap <buffer> i$ <Plug>(textobj-sandwich-query-i)$
xmap <buffer> i$ <Plug>(textobj-sandwich-query-i)$
omap <buffer> a$ <Plug>(textobj-sandwich-query-a)$
xmap <buffer> a$ <Plug>(textobj-sandwich-query-a)$
" Add to predefined sandwich rule 'ma' the trigger character 'd' for delimiter
" in https://github.com/machakann/vim-sandwich/blob/master/ftplugin/initex/sandwich.vim
let b:sandwich_recipes[26].input = ['ma', 'd']
omap <buffer> id <Plug>(textobj-sandwich-query-i)d
xmap <buffer> id <Plug>(textobj-sandwich-query-i)d
omap <buffer> ad <Plug>(textobj-sandwich-query-a)d
xmap <buffer> ad <Plug>(textobj-sandwich-query-a)d
Following things are now possible using only sandwich.vim
ysc/dsc/cscyse/dse/cseysd/dsd/csdys$/ds$textobjects:
ib/ab (automatically select closest surrounding textobject)ic/acie/aei$/a$id/adNow it should be possible to do vadad, vaeae, and vacac.
https://github.com/machakann/vim-sandwich/issues/35
Hypothetical vis (visual inner section) is somewhat equivalent to []v]] and vas would be [[v]]. Since vimtex already provides square-bracket section motions, you might be able to wrap them with text objects. Also check out pandoc-sections.vim for a similar feature (but in pandoc, not latex).
I've just pushed some bug fixes. These should make the vaeae, vadad and vacac work as expected. I'm a little bit unsure, though, if my recent commits also give unexpected behaviour. If so, please let me know and I'll fix it.
Also, thanks @kiryph for giving good information about vim-sandwich. I really like that plugin, and I will look into the new LaTeX buns.
Btw, @kiryph, I would be happy to hear if you have updated settings for LaTeX / vim-sandwich...
Thanks for your update. After having a first look at following cases, vacac, vaeae, and vadad work.
\textbf{\emph{hello}}
\begin{itemize}
\item
\begin{itemize}
\item CURSOR
\end{itemize}
\item
\end{itemize}
$ \left( E = (mc^2) \right) $
That is really great!
Just for the record: my issue with ac regarding commands with several arguments (e.g. \textcolor{red}{hello}) which I have raised here https://github.com/machakann/vim-sandwich/issues/39 exists also for vimtex-textobject ac (except that vimtex handles square brackets such as \chapter[toctitle with spaces]{title}).
Since kana/vim-textobj-fold cannot be extended (vazaz), and often your cursor is in a math, list, or other environment (i.e. you are in a fold you are not interested in) and therefore you cannot directly address the current section/subsection/paragraph to do something with it, it does not feel as 'razor-sharp' as other textobjects.
So I have actually changed my mind about this and would be in favour of a pair of textobjects for the sections.
One should recall that vim provides ap and ip for paragraphs. My suggestion to name such textobjects would be aP and iP.
@lervag What do you think? Are these good enough reasons (extendability and direct addressable) for new textobjects or do you think textobj-fold is sufficient?
I've pushed a PR that implements aP and iP. Please test. I would not be surprised if there are some minor issues, in particular with regard to edge cases. Let's continue the discussion there.
Great!
Thanks for changing your mind! @kiryph thanks for your very useful comments!
Sorry again for being out of discussion for a while.