Code Highlighting - How to highlight code in vim without specifying a file extension
为情所困
为情所困 2017-05-16 16:42:47
0
2
837

When using vim, when I

vim test.php

Specify a file suffix name and it will be highlighted when writing code, but in many cases, vim is used directly when saving

:w test.php

to specify the file name.
Is there any way to make vim recognize the first line of the file? For example, if PHP writes `<?php`, the code can be highlighted

为情所困
为情所困

reply all(2)
世界只因有你

If you want to use a script to automatically detect the first line to determine the file type, I think it is not easy to implement.
But it can be used:

:set syntax=c

(php, java, etc.) Call the highlighted syntax of C (php, java, etc.) language in this file.
Or use

:set filetype=c

(Abbreviation se ft=c) Modify the file type.

曾经蜡笔没有小新

This can still be achieved and can be solved using autocmd, :au BufRead * switchFileTypeManully()
In fact, when you do this, you also need to judge the file type based on the file characteristics, just like the suffix name.
Just like the PE file, the system will judge the file based on the file header and file attributes

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template