The Apache config file for my website is named mywebsite.com. Apparently the extension .com is causing vim to set the wrong file type as dcl which makes the syntax highlighting wrong as well. All of my other host config files are correctly identified as Apache files.

I am able to :set filetype=apache and everything looks great but when after closing vim and coming back to edit the file type is back to dcl again.

Other than renaming my host config file, is there a solution to this? Is there a way to make vim remember the filetype of this one particular file? Or better yet, is there a way to correct this wrong file type detection?

  • Never do anything in /usr/share/vim/: all your custom stuff must sit in your ~/.vimrc file and your ~/.vim/ directory. – romainl Aug 28 '13 at 7:47
  • 2
    1. Everything under /usr/share/vim/ is susceptible to be replaced during the next upgrade; do you want your changes to disappear with no way to get them back? 2. The purpose of that dir is to provide a baseline environment; tinkering with it affects Vim's stability and behavior in unpredictable ways. Your change is probably innocuous but any future change may not be. 3. You have a special place under your control where you can do whatever you want to alter Vim's behavior safely and without needing admin rights, use it. 4. ~ is where you traditionally do all your configuration; use it. – romainl Aug 28 '13 at 12:06
  • thank you. that's some sound reasoning and i'll take your advice on this. – billynoah Aug 28 '13 at 16:43

One way is to add a modeline to the beginning or end of the file. A basic Vim modeline looks like this:

# vim: ft=apache

The comment character itself is ignored by Vim. There is also an alternative syntax that allows following comment characters as well:

/* vim: set ft=apache: */

Emacs and some other editors use -*- mode: apache -*- instead.

If you wanted to override the file type detection by extension, you could add the following to your ~/.vim/filetype.vim:

augroup filetypedetect
  au BufNewFile,BufRead *.com setl ft=apache
augroup END

(I am not sure if setf apache or setl ft=apache is better.)

Note that the detection isn't entirely wrong, it's merely inaccurate. The OpenVMS operating system uses .com as the extension for "command" files – much like shell scripts or batch files – and they're written in the OpenVMS shell language, DCL (DIGITAL Command Language).

  • Thanks very much! I didn't know about modelines. And yes, I vaguely remember that .com extension from scripting DCL on VMS in the 80's. – billynoah Aug 27 '13 at 21:03

How to do this is described in Vim's built-in reference manual under

:help new-filetype

As explained in section C.2., create a file named ~/.vim/filetype.vim with these contents:

if exists("did_load_filetypes")
augroup filetypedetect
    au! BufRead,BufNewFile *.com setfiletype apache
augroup END

That will recognize any file ending in .com as filetype apache. If you want only mywebsite.com to be recognized, change that au line to

au! BufRead,BufNewFile mywebsite.com setfiletype apache

As an addition to both the below answers I'd like to share what I did in the end which is to modify the main config file /usr/share/vim/vim73/filetype.vim from:

au BufNewFile,BufRead *.com call s:BindzoneCheck('dcl')


au BufNewFile,BufRead *.com call s:BindzoneCheck('apache')

I realize this may not be appropriate for every user, particularly if you ever intend to write dcl scripts, but in my case this is fine.

Note: I migrated this from an edit on my question to a proper answer. At the time I wrote it I didn't have enough rep to add an answer, but now I do. User @romainl brought up some valid reasons not to do it this way in the comment on my question above, but at the very least I think this is useful because it helps shed light on where those filetype associations are originate.

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.