简体   繁体   中英

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. When I create a new file with vim and write there some code - no syntax highlight. After saving this file and reopening with vim - syntax highlight works perfect. I am using 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).

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.

This problem shouldnt happen when you do vim some.c or similar, because the extension will force detection based on extension rules.

Vim must know how to highlight your syntax in order to actually highlight it. 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. 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 :

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:

:set filetype=html

Vim will then highlight the syntax of the file as HTML syntax.

More information is available in the help pages .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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