如何强制 vim 将文件语法高亮显示为 html?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3853028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:44:41  来源:igfitidea点击:

How to force vim to syntax-highlight a file as html?

htmlsyntax-highlightingvim

提问by Karthick

How do I set vim's syntax highlighting to treat a file extension as an html file?

如何设置 vim 的语法突出显示以将文件扩展名视为 html 文件?

I'm using ez template, so the file's extension is .ezt. But a lot of it is normal html code.

我使用的是 ez 模板,所以文件的扩展名是.ezt. 但是很多都是普通的html代码。

采纳答案by Benoit

You can also put this into your .vimrc:

你也可以把它放到你的 .vimrc 中:

au BufReadPost *.ezt set syntax=html

回答by Amber

:set syntax=html

回答by slm

Take a look at this Vim wikia topic. Some useful tips:

看看这个Vim wikia 主题。一些有用的提示:

  • As other answers have mentioned, you can use the vim set command to set syntax. :set syntax=<type>where <type>is something like perl, html, php, etc.

  • There is another mechanism that can be used to control syntax highlighting called filetype, or ftfor short. Similar to syntax, you give it a type like this: :set filetype=html. Other filetypes are perl, php, etc.

  • Sometimes vim "forgets" what syntax to use, especially if you're mixing things like php and html together. Use the keyboard shortcut Ctrl+L(<C-L>) to get vim to refresh the highlighting.

  • 正如其他答案所提到的,您可以使用 vim set 命令来设置语法。:set syntax=<type>这里<type>是一样的东西perlhtmlphp,等。

  • 还有另一种机制可用于控制语法高亮,称为filetype,或ft简称。句法相似,你给它一个类型是这样的::set filetype=html。其它文件类型是perlphp

  • 有时 vim 会“忘记”要使用什么语法,尤其是当您将 php 和 html 之类的东西混合在一起时。使用键盘快捷键Ctrl+ L( <C-L>) 让 vim 刷新突出显示。

回答by Daniel

Note that :set syntax=xmlhighlights properly but seems to fail when one is attempting to autoindent the file (i.e. running gg=G).

请注意,:set syntax=xml突出显示正确但在尝试自动缩进文件(即运行gg=G)时似乎失败。

When I switched to :set filetype=xml, the highlighting worked properly and the file indented properly.

当我切换到 时:set filetype=xml,突出显示正常工作并且文件缩进正常。

回答by Ярослав Рахматуллин

In a .php file (or a html file), you could use a Vim Modelineto force certain commands or settings:

在 .php 文件(或 html 文件)中,您可以使用Vim Modeline来强制执行某些命令或设置:

 1 /* vim: syntax=javascript
 2  *
 3  * .submit_norefresh()
 ~
 ~

回答by wisbucky

To make it automatic, add this line to your ~/.vimrc:

要使其自动,请将此行添加到您的~/.vimrc

autocmd BufNewFile,BufRead *.ezt set filetype=html

autocmd BufNewFile,BufRead *.ezt set filetype=html

If you want to just do it for the current file, then type:

如果您只想对当前文件执行此操作,请键入:

:set filetype=html

:set filetype=html

You could also substitute syntaxinstead of filetype, but filetypeaffects more things than syntax(including syntax highlighting, indenting rules, and plugins), so generally you should use filetypeunless you only want to affect syntax.

你也可以用syntax代替filetype,但filetype影响比syntax(包括语法高亮、缩进规则和插件)更多的东西,所以通常你应该使用,filetype除非你只想影响syntax.