开发者

Create Vim file type for beamer presentations that modifies tex file type based on file extension and content of first few lines

When I write beamer tex slide presentations, I have developed a few modifications to the tex filetype that I would like to operate by default on beamer documents.

When I create a beamer document it has a .tex file extension and a line something like the following in the first few lines:

\documentclass[...]{beamer}

For simplicity, we could say that any file with a .tex extension and the text beamer anywhere in the first 5 lines should default to filetype beamer.

  • How could I automatically set the filetype to beamer using both information from the file extension and the initial few lines o开发者_运维技巧f the file?

As often happens, the process of formulating the question has led me to :h new-filetype-scripts, but this still requires a fair amount of deciphering. Any tips would be most welcome.


According to the help article you pointed out, you need to create a scripts.vim file in your vim runtime directory with content that looks like this:

if did_filetype()
  finish
endif

if expand('%:e') == 'tex'
  for n in range(1, 5)
    if getline(n) =~ 'beamer'
      setfiletype beamer
      break
    endif
  endfor
endif

Interestingly enough, this doesn't work for me. I managed to do it by putting it in ftplugin/beamer.vim and changing it a bit to the following:

for n in range(1, 5)
  if getline(n) =~ 'beamer'
    set filetype=beamer
    break
  endif
endfor

Whenever the tex filetype gets loaded, the beamer-specific stuff gets sourced as well. I'd recommend trying the scripts.vim approach first, and consider the second one if you can't get it working that way.


I'm still experimenting but a preliminary solution seems to be to add a file called beamer.vim to ~/.vim/ftdetect with the following command:

au BufRead,BufNewfile *.tex if getline(1) =~ 'beamer' | set filetype=beamer.tex | endif  

Note, I've simplified the command slightly from specifications above, and filetype beamer.tex incorporates both the tex features and my beamer customisations.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜