I have a lot of *.sage that follow the Python syntax. However, VIm does not recognize this. Can I make VIm set ++filetype based solely on the file extension?


You can add an ftdetect plugin for vim. Read :h ftdetect for detailed information. To quote from the docs for the lazy :

If a file type that you want to use is not detected yet, there are four ways to add it. In any way, it's better not to modify the $VIMRUNTIME/filetype.vim file. It will be overwritten when installing a new version of Vim.

A. If you want to overrule all default file type checks. This works by writing one file for each filetype. The disadvantage is that means there can be many files. The advantage is that you can simply drop this file in the right directory to make it work.

  1. Create your user runtime directory. You would normally use the first item of the 'runtimepath' option. Then create the directory "ftdetect" inside it. Example for Unix:

    :!mkdir ~/.vim
    :!mkdir ~/.vim/ftdetect
  2. Create a file that contains an autocommand to detect the file type. Example:

    au BufRead,BufNewFile *.mine      set filetype=mine

    Note that there is no "augroup" command, this has already been done when sourcing your file. You could also use the pattern "*" and then check the contents of the file to recognize it. Write this file as "mine.vim" in the "ftdetect" directory in your user runtime directory. For example, for Unix:

    :w ~/.vim/ftdetect/mine.vim
  3. To use the new filetype detection you must restart Vim. The files in the "ftdetect" directory are used after all the default checks, thus they can overrule a previously detected file type. But you can also use :setfiletype to keep a previously detected filetype.


Yes, you can, using the au(tocmd) event handler for events BufNewFile and BufRead:

au BufNewFile,BufRead *.sage set filetype=python

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.