8

I'm writing LaTeX code which has a lot of sections like this

\begin{definition}
blah blah blah ...
...
\end{definition}    

In normal text, I'm very fond of gq] to rewrap everything nicely. Inside a block section like the above, that wraps the end command too, which I'd rather keep on a separate line.

Is there a way to tell vim that a line starting \end ends a paragraph? Or is there another formatting command I can use (I have <F8> mapped to gq], I don't mind redefnining it?

6

One way to do it employs the ie (inside-environment) custom text object, available in a number of places: e.g. in the plugin vimtex, or with vim-textobj-latex (and there are others). With this functionality, then the rewrap command becomes:

gqie

or

gwie

(to maintain cursor position).

If you only want to reformat text from current text position (I see that this is your use case) to the paragraph end, you can probably make do with the following construct (no plugins required):

gq/\(^\s*$\|\\end\)

This will format to either an empty line, or a line containing the \end keyword, whichever comes first. So you could use this mapping:

:nnoremap <F8> gq/\(\\end\\|^\s*$\)<CR>
  • gqie does what I want when inside a begin...end block, in the main text body it tries to wrap everything until \end{document} - is there a way to tell it to stop at the next blank line? – Bristol Aug 3 '15 at 13:53
  • Well, outside an environment - maybe the default gqap ? – VanLaser Aug 3 '15 at 13:54
  • See the updated answer, for a possible F8 remapping. – VanLaser Aug 3 '15 at 14:19
  • I presume I have a typo somewhere as the following doesn't work: :map <F8> gq/\(^$\|^[%]$\|^\\end\)<CR>. Can anyone tell me what I'm doing wrong? I'm trying to match a completely empty line (^$), a line with only a % comment character (^[%]$) or a line starting \end (^\\end). – Bristol Aug 3 '15 at 14:23
  • I think you must use \\| as separator instead of \| when mapping. – VanLaser Aug 3 '15 at 14:25

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.