Wednesday, March 05, 2008

Vim-LaTeX, Vim 7.0, and the filetype plugin

Have you noticed that Vim-LaTeX sometimes does not automatically load when you open up a new .tex file?

If you're using Vim-LaTeX with Vim 7 or higher, you may want make sure you have this somewhere in your .vimrc:
" Prevents Vim 7.0 from setting filetype to 'plaintex'
let g:tex_flavor='latex'
Otherwise, some of your .tex files will not automatically kick off vim-latex on opening! In particular, vim-latex is usually only loaded if Vim finds a \documentclass, \begin, or \usepackage in the first 1000 uncommented lines of a .tex file. This change forces it to load for all .tex files.

So, if you're having problems getting Vim-LaTeX to load when Vim opens a .tex file, try making that change (as well as all of the filetype plugin changes to your .vimrc that are documented).

Some alternative solutions:
  • ALTERNATIVE 1: If you don't want to make that change, you can add
    %&latex
    to the top of any .tex file that you want to force to load vim-latex. That is, Vim will still default to using the plaintex flavor unless it sees that comment or commands like \documentclass, \begin, or \usepackage.

  • ALTERNATIVE 2: You can actually add
    % vim: filetype=tex
    OR
    % vim: ft=tex
    to ANY file to force vim-latex to load. (note: you may need to have the modeline option turned on in Vim for this to work)

  • ALTERNATIVE 3: While editing ANY file, you can execute the Vim command (in command mode):
    :set filetype=tex
    OR
    :set ft=tex
    to load vim-latex at that instant.
This post was updated on August 7, 2008.

10 comments:

Unknown said...

Thank you-- I'd noticed the odd behavior and had just skipped around it by running gVim (which didn't seem to suffer the same problems).

Ted Pavlic said...

My GUESS is that you are using an old version of gVim that is based on Vim6.0. If you update to the latest version of gVim, which is based on Vim7, you'll have the same problem.

In either case, fixing the tex_flavor will fix the "problem."

Unknown said...
This comment has been removed by the author.
Unknown said...

Thank you for this post. It saved my day. Now when I run "gvim newfile.tex" it loads the menus. But still I cannot get the prepared preamble. The new file is completely empty /-:

Ted Pavlic said...

Sven -- You should try

:help latex-suite

and read some of the instructions there. The templates aren't supposed to show up automatically (though, you could configure Vim to do that). However, there are lots of shortcuts.

For example, in insert mode, type:

EDO

You'll get a simple document class setup. Hit CNTRL+J to jump from fill-in-area to fill-in-area.

Or, try typing:

:TTemplate

without any arguments. You'll get a menu with a few sample templates. You can choose one. In fact, you could choose one by passing an argument directly:

:TTemplate article

Check out the latex-suite documentation (:help latex-suite) for more information. In particular, check out chapter 3.

Additionally, try doing

:help latex

You should find a pretty comprehensive LaTeX manual.

Unknown said...

Any idea how to avoid extra whitespace in custom templates stored within ~/.vim/ftplugin/latex-suite/templates
?

The default templates all work well, but when I load up a custom template I see lots of extra indentation (similar to what happens when pasting into vim without using ':set paste').

Ted Pavlic said...

Rhys -- What do you have your "shiftwidth" set to? Type:

:set sw?

That's the number of spaces used for the autoindenting. Try doing:

:set sw=2

and then calling up a template. I think that indenting will get fixed. Type:

:help shiftwidth

for more information.

Unknown said...

Shiftwidth doesn't seem to have any effect. For a template like this example
I see results like this.

I see the same behavior if my template uses either leading spaces or tab characters. From looking through the presupplied templates that ship with latex-suite, it seems none of them have leading line indentation.

Ted Pavlic said...

OK. Your example helped me see what was going on. I thought you were exaggerating about the "set paste," but you actually ARE getting stairstepping.

You should notice that the pre-packaged Vim-LaTeX templates do not have *any* indenting, and so they don't have the problem. That is, if you do:

:TTemplate IEEEtran

the resulting text does not get stairstepped. That is, it's indented with your "shiftwidth" settings.

In your case, you've put your indenting within your template. That's the problem.

If you actually issue:

:set paste

it will fix the problem. That is,

:TTemplate stuff

will give you the output you want (where indents are set by "shiftwidth," as explained).

Otherwise, you should remove the indenting from your template.

Vim-LaTeX could be changed so that it automatically does a ":set paste" and ":set nopaste" before and after the :TTemplate call, but I think the authors were worried that people would have one indenting convention (tabs, spaces, and different shiftwidths) and the template would have another. So, by stripping out indenting, they let Vim indenting based on how you've set it up to indent.

Does that help?

Unknown said...

Using :set paste before :TTemplate works like a charm. Thank you.