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

latexvim

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?

Best Answer

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

formatting your example produces this:

\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.

Related Question