Wikia

Vim Tips Wiki

Watchlist Recent changes

Customize ftplugin, syntax and more - example for TeX



Tip 627 Printable Monobook Previous Next

created December 24, 2003 · complexity basic · author Suresh Govindachar · version 6.0


It is possible to customize the default environment that vim provides (based on filetype). In older versions of vim (5.x), this was done with a "myfiletypefile" file. In newer versions, one customizes by adding files to the appropriate subdirectory under the vimfiles directory.

For example, to customize the default TeX environment (of 6.1), I added the following ftplugin and syntax files:

"--------------------------
" vimfiles/ftplugin/tex.vim
"--------------------------
if exists("b:did_myftplugin")
  finish
endif
let b:did_myftplugin = 1

" Set 'formatoptions' to break comment lines but not other lines,
" and insert the comment leader when hitting <CR> or using "o".
setlocal fo-=t fo+=croql

" Allow "[d" to be used to find a macro definition:
" Recognize plain TeX \def, \gdef, \let and \font
setlocal define=\\\\def\\\\|\\\\gdef\\\\|\\\\let\\\\|\\\\font\\\\|\\\\\\(re\\)\\=newcommand{
"--------------------------
" vimfiles/syntax/tex.vim
"--------------------------
if exists("b:did_mysyntax")
  finish
endif
let b:did_mysyntax = 1
"hilight 1 true in, 1truein, 1 truein, 1true in, 1in, 1 in, 1.1 truein, etc.
syn match texTrueLength "\<\d\+\(\.\d\+\)\=\(\ *true\ *\|\ *\)\
      \(\ *pt\|\
      \ *he\|\
      \ *bp\|\
      \ *cc\|\
      \ *cm\|\
      \ *dd\|\
      \ *em\|\
      \ *ex\|\
      \ *in\|\
      \ *mm\|\
      \ *pc\|\
      \ *pt\|\
      \ *sp\)\>"

if version >= 508 || !exists("did_c_syn_inits")
  if version < 508
    let did_c_syn_inits = 1
    command -nargs=+ HiLink hi link <args>
  else
    command -nargs=+ HiLink hi def link <args>
  endif
  HiLink texTrueLength Number
  delcommand HiLink
endif

CommentsEdit

The first four lines in each file should _NOT_ be present:

if exists("b:did_my...")
finish
endif
let b:did_my... = 1

When these lines are present, they prevent the loading of the customization when, for example, the user does ":e!".


Unless you add

let b:did_ftplugin

to the ftplugin, the default ftplugin/tex.vim will still be read, and it will clobber your customizations. Similar remarks apply to the other customization files. See :help ftplugin-overrule for more details.

This can be useful as an example, but before you decide not to use the default settings, have a look at what you are losing. Because of a typo, the default ftplugin/tex.vim does not recognize \gdef, but it does recognize \def, \let, \font, \newcommand, and \renewcommand, as well as \futurelet, \newcount, and several other variants. If you care about \gdef, the best thing to do is to alert the maintainer (in this case, c'est moi).


Isn't this tip deprecated -- when we look at the (La)TeX aspect? It may be best to have in replacement Category:Scripting tips about:

  • How to override the default syntax?
  • How to override the default ftplugin?

And

  • What to do when I found a bug in a script from Vim distribution?

Pages on Vim Tips Wiki

Add a Page
1,590pages on
this wiki
Advertisement | Your ad here

Latest Photos

Add a Photo
71photos on this wiki
See more >

Recent Wiki Activity

See more >

Around Wikia's network

Random Wiki