Changing filetype based on file extention in vim

邮差的信 提交于 2019-12-02 01:21:41

Wondering whether this line goes before or after filetype … on. In the former case you should try putting it (your autocommand) after this line. Better if you put it into ~/.vim/ftdetect/markdown.vim and use setfiletype markdown instead of setlocal ft=markdown:

augroup filetypedetect
    autocmd BufNew,BufNewFile,BufRead *.txt,*.text,*.md,*.markdown :setfiletype markdown
augroup END

: it is the default way of doing such things. ~/.vim must go before /usr/share/vim/* paths in 'runtimepath' option in this case (it does by default).

I created a ~/vim/ftdetect/markdown.vim file with this line

autocmd BufNewFile,BufRead *.md,*.mkdn,*.markdown :set filetype=markdown

Reading the docs for filetype, setfiletype only sets if filetype is unset. So you need to use set for an unconditional change to a filetype.

I was able to get syntax highlighting for alternate file extensions by creating renamed copies of the target syntax file in the Vim\vim74\syntax directory.

To make *.md open as a .markdown:

copy markdown.vim md.vim

or paste a copy of markdown.vim to the syntax folder, then rename the copy md.vim.

(Running vim74 on win7)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!