2

Is there an easy way to wrap LaTeX source? Using gwip is great for individual paragraphs,

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur.

...but it fails more generally for other situations such as itemize environments:

\begin{itemize}
  \item Lorem ipsum dolor sit amet, consectetur adipiscing
    elit, sed do eiusmod tempor incididunt ut labore et
    dolore magna aliqua.
  \item Ut enim ad minim veniam, quis nostrud exercitation
    ullamco laboris nisi ut aliquip ex ea commodo consequat.
  \item Duis aute irure dolor in reprehenderit in voluptate
    velit esse cillum dolore eu fugiat nulla pariatur.
\end{itemize}

Or captions:

\begin{figure}
  \caption[Lorem ipsum]{%
    Lorem ipsum dolor sit amet, consectetur adipiscing elit,
    sed do eiusmod tempor incididunt ut labore et dolore
    magna aliqua. Ut enim ad minim veniam, quis nostrud
    exercitation ullamco laboris nisi ut aliquip ex ea
    commodo consequat. Duis aute irure dolor in
    reprehenderit in voluptate velit esse cillum dolore eu
    fugiat nulla pariatur.%
  }
\end{figure}

Issues:

  • Using gw in visual mode is not very fast. (gwip with the ip text-object is far easier and doesn't require manual selection.)
  • Using textwidth=80 is great for auto-wrapping when writing a fresh paragraph, but not very helpful when editing text in the middle of a paragraph.
  • Autowrapping within a middle of a paragraph can be accomplished with set formatoptions+=a, but that also ends up wrapping everything after the paragraph too:

    \begin{figure}
      \caption[Lorem ipsum]{%
        Lorem ipsum dolor sit amet, consectetur adipiscing elit,
        sed do eiusmod tempor incididunt ut labore et dolore
        magna aliqua.% } \end{figure}
    

1 Answer 1

2

The vimtex plug-in can help here.

Among its features you'll find it support custom text objects:

  • ic ac Commands
  • id ad Delimiters
  • ie ae LaTeX environments
  • i$ a$ Inline math structures
  • iP aP Sections

In your case, it seems to me that gwie would do what you're looking for, selecting the text inside an environment, which is the block delimited by \begin{...} and \end{...}.

If you also install the targets.vim plug-in, you can enable additional text objects for environments, see targets-textobj-cheatsheet.md.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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