Home  >  Q&A  >  body text

Code Highlighting - How to highlight code in vim without specifying a file extension

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

为情所困为情所困2712 days ago786

reply all(2)I'll reply

  • 世界只因有你

    世界只因有你2017-05-16 16:44:47

    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.

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-16 16:44:47

    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

    reply
    0
  • Cancelreply