In vim, why my autocmd setfiletype command not working?

Tag: vim Author: czz8111 Date: 2013-06-11

In my .vimrc I add this,

autocmd BufNewFile,BufRead *.markdown setfiletype octopress

But it seems not working because after I open a xxx.markdown file and input the command setfiletype octopress everything works fine.

Here is my intact .vimrc

set nocompatible
syntax on
filetype off
colorscheme desert
set nu
set mouse=a
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
Bundle 'gmarik/vundle'
filetype plugin indent on    

Bundle 'vim-octopress'
autocmd FileType markdown setfiletype octopress

Hope someone can help me and tell me how to debug this thing...

Best Answer

I don't see this:

autocmd BufNewFile,BufRead *.markdown setfiletype octopress

I only see this at the end:

autocmd FileType markdown setfiletype octopress

I think fixing that will fix you issue.


A couple more things to consider. First, the Markdown-syntax plugin sets the filetype to be mkd, not markdown. This doesn't seem to work correctly either:

autocmd FileType mkd setfiletype octopress

But this does:

au FileType mkd set filetype=octopress

...And that makes sense now. setfiletype won't set the file type if it's already been set. Since it was already flagged as being of type mkd, it wasn't being updated to the new file type.


I tried. Not working also.
Is the filetype being set correctly with the first version? I setup a vimrc with only autocmd BufNewFile,BufRead *.markdown setfiletype octopress in it, and then ran vim -u ./my-vimrc -U NONE xxx.markdown. Typing :set filetype? showed that it was properly set to octopress.
I know it works. I guess it's something in the .vimrc makes this command not working.
Step one is to verify that the filetype is being set correctly with your autocmd. I know it works when you say setfiletype octopress manually, but does the file have the correct file type without doing that? I don't think it is.
What's going on. I change setfiletype octopress to set filetype=octopress and it works well. Thank you anyway.

Other Answer1

It's because setfiletype ... but only if not done yet in a sequence of (nested) autocommands.

Doc about setfiletype function