# vim “gq}” command to re-wrap paragraph, and LaTeX

How can I make this command not wrap things like \item?

For example:

\begin{enumerate}
\item this might be a long paragraph that spans multiple lines
\item blah
\end{enumerate}


if I run "gq}" with the cursor on the first item (first letter of this), it produces this output:

\begin{enumerate}
\item this might be a long
paragraph that spans multiple
lines \item blah \end{enumerate}


This is what I want:

\begin{enumerate}
\item this might be a long
paragraph that spans multiple
lines
\item blah
\end{enumerate}


If I can somehow get vim to recognize that the token \item signifies the end of a paragraph, I'm thinking it will work correctly. How would I go about doing that, though?

-

I think you can do this with the 'formatlistpat' option set to include the paragraph-delimiting tags. You need to include 'n' in 'formatoptions' for that to work. See

:help 'formatlistpat'
:help 'formatoptions'


For example, with

:set formatlistpat=^\\s*\\\\\$$end\\\\|item\$$\\>
:set formatoptions+=n
:set textwidth=40


\begin{enumerate}
\item this might be a long paragraph
that spans multiple lines
\item blah
\end{enumerate}


Note that the second line of the first item is indented by the length of the \item tag, so it's not exactly what you wanted, but it might be close enough. You might also experiment with \ze as mentioned in the help to see if you can get Vim to recognize the tag without indenting the subsequent lines. I didn't try that.

Update

The 'formatlistpat' is a little clearer, and easier to edit, if it is set using :let instead of :set.

:let &l:flp = '^\s*\\$$end\|item$$\>'


The l: portion specifies that it is a buffer-local setting.

Update

To reduce the indent down to 1 character, do this:

:let &l:flp = '^\s*\\\ze$$end\|item$$\>'


Note that if there are any white spaces before the \item, then the indent becomes that number + 1. I don't know if it's possible to reduce the indent down to 0 using the \ze.

-
Cool! Is there a way to only use a custom formatlistpat for specific file extensions? I feel like it may be cleaner to only recognize \item as a paragraph-delimiting tag in files that end with the .tex extension. Also, why are there so many \ characters? –  orryowr May 9 '12 at 2:56
:help 'formatlistpat' says that the option is local to buffer, so you could put that setting in ~/.vim/after/ftplugin/tex.vim and use setlocal instead of set and it would affect only .tex files. The \ characters are just the way special characters have to be escaped on the rhs of a set command. If I did any more with that setting, I would set it using let &flp = "stuff" rather than set flp=stuff. The quoting and escaping on the rhs of a let is much easier. See :help :let-&. The branch separator is \|. That has to escaped for some reason as \\|. The set set requires –  garyjohn May 9 '12 at 3:16
each of those to be escaped, so you wind up with \\\\|. I don't have a good explanation of why. –  garyjohn May 9 '12 at 3:18
I'm unable to reproduce the formatting you've done. You used vim and the command gq}, right? So far I'm doing :set tw=40 and :set formatlistpat=^\\s*\\\\\$$end\\\\|item\$$\\>, but it's still acting the same as before. (Also, what does the \\> at the end do?) –  orryowr May 9 '12 at 3:22
OK, I opened a new buffer, copied your text into it, copied the 'formatlist' setting from my answer above, :set fo+=n tw=40, moved the cursor to in front of \begin, and typed gq}. It worked just as before. I was using gvim 7.3.487 on Linux. –  garyjohn May 9 '12 at 4:22