简体   繁体   English

Vim 语法没有实时突出显示

[英]Vim syntax doesn't highlight in real time

I have enabled vim syntax on (in ~/.vimrc syntax on) and it works but only on files with a code in when I view them.我已经启用了 vim 语法(在 ~/.vimrc 语法上),它可以工作,但仅在我查看带有代码的文件上有效。 When I create a new file with vim and write there some code - no syntax highlight.当我用 vim 创建一个新文件并在那里写一些代码时 - 没有语法高亮。 After saving this file and reopening with vim - syntax highlight works perfect.保存此文件并使用 vim 重新打开后 - 语法高亮效果完美。 I am using manjaro KDE.我正在使用 manjaro KDE。

When you open a new file without an extension ( vim mynewfile ) none of vim's filetype detection mechanisms can recognize it (they all use either extensions or first-couple-of-lines heuristics, which don't work here).当您打开一个没有扩展名的新文件 ( vim mynewfile ) 时,vim 的文件类型检测机制都无法识别它(它们都使用扩展名或首行启发式算法,这在此处不起作用)。

When you enter code and reopen the file, the line-checks for filetypes work, causing the syntax to be set correctly, causing highlights to apply.当您输入代码并重新打开文件时,文件类型的行检查会起作用,从而正确设置语法,从而应用高亮显示。

You can always set syntax=mine (though set filetype=mine is better) to set it manually.您始终可以set syntax=mine (尽管set filetype=mine更好)来手动设置它。

This problem shouldnt happen when you do vim some.c or similar, because the extension will force detection based on extension rules.当您执行vim some.c或类似时,不应发生此问题,因为扩展将强制基于扩展规则进行检测。

Vim must know how to highlight your syntax in order to actually highlight it. Vim 必须知道如何突出显示您的语法才能真正突出它。 One way to do this, is for Vim to check the file name and sometimes inspect the contents of the file, and set the file type.一种方法是让 Vim 检查文件名,有时检查文件的内容,并设置文件类型。 The file type is then used to highlight the syntax.然后使用文件类型突出显示语法。

To enable detection of the file type (and load plugin and indent files), add the following to your vimrc :要启用文件类型检测(并加载插件和缩进文件),请将以下内容添加到您的vimrc

filetype on plugin indent

If Vim is unable to detect the file type, and you have not yet saved your file with a known extension, you can set the file type manually, like this:如果 Vim 无法检测到文件类型,并且您尚未使用已知扩展名保存文件,则可以手动设置文件类型,如下所示:

:set filetype=html

Vim will then highlight the syntax of the file as HTML syntax. Vim 然后将突出显示文件的语法为 HTML 语法。

More information is available in the help pages .帮助页面中提供了更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM