vim default syntax for files with no extension

こ雲淡風輕ζ 提交于 2020-01-21 06:22:26

问题


How do I set up a default syntax for files that have no extension in vim?


回答1:


One way would be to add an autocommand to your .vimrc for files that don't have the syntax set:

au BufNewFile,BufRead * if &syntax == '' | set syntax=html | endif

Or, you could set the filetype for any file that it's not defined for:

filetype plugin on
au BufNewFile,BufRead * if &ft == '' | set ft=html | endif

Setting filetype plugin on along with the au command gives the added benefit of loading HTML plugins if you have any. This also sets the syntax to "html" as well.




回答2:


To pick the default syntax for files without an extension, you can create an autocommand that checks if the filename contains a ., and if not, switches to the desired syntax:

autocmd BufNewFile,BufRead * if expand('%:t') !~ '\.' | set syntax=perl | endif

This one picks perl as a default syntax, but you can simply use whichever is appropriate.




回答3:


If I remeber it right you can put a file named syntax.vim inside your ~/.vim/syntax folder. This file is used as default syntax highlight source. If your .vim folder does not exist, you have to create it:

mkdir ~/.vim
mkdir ~/.vim/syntax
touch ~/.vim/syntax/syntax.vim

Now you can add your default syntax to the syntax.vim file. For further documentation you can look at the vim sourceforge page.

Hope this helps.



来源:https://stackoverflow.com/questions/2666551/vim-default-syntax-for-files-with-no-extension

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