在vim中根据文件扩展名更改文件类型 [英] Changing filetype based on file extention in vim

查看:62
本文介绍了在vim中根据文件扩展名更改文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据 vim 中的文件扩展名更改文件类型.

I want to change the filetype based on file extension in vim.

我的 .vimrc

autocmd BufNew,BufNewFile,BufRead *.txt,*.text,*.md,*.markdown setlocal ft=markdown

但是当我打开一个扩展名为 .md 的文件时,文件类型没有改变.我运行 :set ft 命令,它将输出显示为 filetype=modula2.

But when I open a file with the extention .md file, the filetype is not changed. I run :set ft command and it shows the output as filetype=modula2.

我做错了什么吗?

我通过重命名我的旧 .vimrc 文件开始调试,并用这一行创建了一个新文件.它工作正常.然后我替换了旧的 .vimrc 文件,一切似乎都运行良好.猜猜这是因为我正在使用的某个插件中存在一些问题.

I started to debug by renaming my old .vimrc file and created a new one with just this line. It was working properly. Then I replaced my old .vimrc file and everything seems to be working fine. Guess it was because of some issues in some addon which I am using.

但接受 ZyX 的回答,因为它认为我是另一种方法.

But accepting ZyX's answer, since it thought me an alternate way to do this.

推荐答案

想知道这一行是在 filetype ... on 之前还是之后.在前一种情况下,您应该尝试将它(您的自动命令)放在这一行之后.如果你把它放入 ~/.vim/ftdetect/markdown.vim 并使用 setfiletype markdown 而不是 setlocal ft=markdown 会更好:>

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

:这是做这些事情的默认方式.在这种情况下,~/.vim 必须在 'runtimepath' 选项中的 /usr/share/vim/* 路径之前).

: 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).

这篇关于在vim中根据文件扩展名更改文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆