Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm not sure whether this is an appropriate question for tex.SE. If you think questions like this should not be asked here, please voice you concern. If necessary I'll open a question on meta.

For readability I like to keep line lengths in my source files at about 80 characters. Vim has the nice shortcut gqap that reflows the current paragraph to fit within 80 characters without wasting space with lines that are too short. It even keeps indentation. Unfortunately it considers paragraphs to be marked by empty lines and considers everything that is not separated by an empty line to be in the same paragraph. In particular any equations (started with \[ or \begin{...}) are always considered to be part of the paragraph and reflowed.

Is there any way to have vim handle LaTeX syntax more intelligently in this respect? Are there any other text editors which can to that?

(I know that I can highlight only the text and then use gq, but if possible I'd like to have a single command to reflow a paragraph without the need to manually mark what a paragraph is.)

share|improve this question
If I understood correctly, does gqq do what you need? (or Vgq). It's line-wise, ie. if you type-in text without manual line-breaks. – morbusg May 25 '11 at 3:11

2 Answers

up vote 17 down vote accepted

I have the following function in my $VIM/ftplugin/context.vim file to format ConTeXt paragraphs (same as LaTeX: the environments are enclosed in \start... and \stop... instead of \begin{...} and \end{...}. It should be easy to adapt this to LaTeX (In fact, I think that I copied it originally from someone who had written it for LaTeX and adapted it to ConTeXt).

" Reformat lines (getting the spacing correct) {{{
fun! TeX_fmt()
    if (getline(".") != "")
    let save_cursor = getpos(".")
        let op_wrapscan = &wrapscan
        set nowrapscan
        let par_begin = '^\(%D\)\=\s*\($\|\\start\|\\stop\|\\Start\|\\Stop\|\\\(sub\)*section\>\|\\item\>\|\\NC\>\|\\blank\>\|\\noindent\>\)'
        let par_end   = '^\(%D\)\=\s*\($\|\\start\|\\stop\|\\Start\|\\Stop\|\\place\|\\\(sub\)*section\>\|\\item\>\|\\NC\>\|\\blank\>\)'
    try
      exe '?'.par_begin.'?+'
    catch /E384/
      1
    endtry
        norm V
    try
      exe '/'.par_end.'/-'
    catch /E385/
      $
    endtry
    norm gq
        let &wrapscan = op_wrapscan
    call setpos('.', save_cursor) 
    endif
endfun

nmap Q :call TeX_fmt()<CR>
share|improve this answer
1  
Thank you, this works really well. For LaTeX one needs only needs to replace \\start\|\\stop\|\\Start\|\\Stop\| by |\\begin\|\\end\|\\[\|\\]\|. (Also the {{{ should be ended by a " }}} after the last line.) – Caramdir Aug 11 '10 at 17:35
1  
I'm not having success with the pattern \\[ and \\]. In my case, Vim seems to simply ignore display math that starts and ends with \[ and \]. I'm no expert in Vim regular expressions but I found that escaping the square brackets does the trick, i.e., replacing \\[ with \\\[ and \\] with \\\]. – Dominique Jan 2 '12 at 22:49

The following solution only applies to paragraph formatting, it will properly work depending on the LaTeX styling settings.

Another possible solution would be to set a hard wrap of 80 characters.

http://vimcasts.org/episodes/hard-wrapping-text/

formatoptions:

t - Auto-wrap text using textwidth

c - Auto-wrap comments using textwidth, inserting the current comment leader automatically.

a - Automatic formatting of paragraphs.

Here are some of the commands to set it up:

:set formatoptions=tc
:set fo+=a
:set textwidth=80

Tip: Use the autocmd or ftplugin folder to setup these settings automatically according to filetype. Run help: ftplugin in vim for more info.

And read here for more information about formatting:

http://vimdoc.sourceforge.net/htmldoc/change.html#fo-table

share|improve this answer
With this method, you can't end lines with a comment. – sappjw Dec 13 '12 at 16:12

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.