Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
    
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
1  
: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
1  
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
show 3 more comments

Here is solution that works for me. I've wrote an external script to handle the formatting of latex text (https://github.com/elmanuelito/par-latex)

Example of usage in vim:

  • Select some text (e.g. Vj or ggVG)
  • type ":!par-latex" (vim command-line looks like: '<,'>!par-latex)
  • type enter

With default options, it will handle your example if you select the text within the itemize environment. For it to work when you select more text, you'll need to activate the option format_inside_group at the beginning of the script.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.