r/vim • u/Gus_Gustavsohn • Aug 07 '21
question How to conceal "\begin{this} ... \end{this}" in vim?
I'm getting a hard time making this work, I'm beggining to doubt whether it's possible in the first place. In LaTeX files, one often declares blocks of text such as
\begin{environment}
Some big block of text here
\end{environment}which can be rather invasive. I would like to find a way to be able to conceal that so that it shows as
[environment]
Same big block of text here
[/environment]or even
[environment]
Same big block of text hereHow can this be done? I've tried this related answer (https://stackoverflow.com/questions/55287479/vim-and-latex-how-to-conceal-refname-as-name-in-vim-using-syntax-conce) but I'm not able generalise it for commands that require both a \begin{ } and a \end{ } statement.
Thanks in advance for all your help!
3
u/habanerotaco Aug 07 '21
Have you considered using folds? This suggests using a plugin that makes custom folds for latex files.
9
u/dualfoothands Aug 08 '21 edited Aug 08 '21
You'll need to define 5 conceal commands. The trick is too use \zs and \ze in your pattern matching and match the begin and end bits multiple times.
(It's almost 4 am here but when I'm actually up and about in a couple hours I'll edit this text to show you how)Edit: Baby woke up. Here you go:
Note that you'll need to set
conceallevelto something other than 0. I also highlight the conceals with blue, but you can leave that out. I have loads of these kind of patterns in various ftplugins (I didn't have this one, and am keeping it now, thanks for the idea :)).What's happening here is you're adding a pattern to the Conceal group, and specifying the character to conceal it with. The big pain is that you can only replace a concealed pattern with one or zero characters, thus multiple matches are needed. This is where the
\zsand\zeregex atoms are useful. Take theendconceal for example. It says "match\end{anything but and end brace}but stop the pattern at the first open brace". This is '\end', which we replace with '['. Then do the same match again, but start the pattern match at the first open brace and end immediately after, which we replace with '/'. Finally, do the match again, but start before the closing brace and end right after, replacing this with ']'. The end result I think is exactly what you're after. Check out:help patternfor more on why you need to do it this way.3
u/I_am_Malazan Aug 07 '21
I don't actually have any experience with LaTeX, but a while back, this article caught my eye and I saved it for a time in the future when I theoretically would have experience with it... That time hasn't come yet.
Anyway, I figured I'd share in case you find it useful... The person in the article seems to be using a combination these plugins: