Search code examples
vimsyntax-highlightingfile-typevim-syntax-highlighting

Detect filetype in vim without valid file extension


I'm looking for a way to determine the filetype of a file in vim and set the syntax highlighting based on the filetype. The only catch is I cannot use the file extension for determining the filetype.

This is my scenario: I use vimdiff or gvimdiff as my P4DIFF tool, which shows the changes between the files in my local copy and the ones from the perforce server. Perforce seems to bring in the files from the perforce server into the /tmp directory and uses the PID to name the file, for example:

/tmp/tmp.24673.23

This was for a C++ source file.

The most frequent filetypes I encounter in the perforce repository are C/C++ sources and header files, Makefiles, python scripts, perl scripts, ruby scripts, and tcl scripts.

  • I've looked into using modeline, but most of the sources in our tree do not have this information embedded in the file.

  • This post mentions about a possible approach to search and identify a magic pattern. I could not find any consistent magic pattern that I could get a high success rate with.

  • Tried using the file binary on my linux box to see what results I get. It seems to identify C/C++ sources well, but fails for Makefiles and even python scripts (which don't have the hashbang)

One good thing is that, among the 2 files that are compared, the file on the right is from my local copy and hence has the correct file name with extension, thereby syntax highlighting is enabled correctly for the file on the right.

Is there a way I could leverage this to set the same syntax highlighting for the file displayed on the left ?

Any alternate solutions to this problem are also welcome.


Solution

  • This was an interesting puzzle. :)

    aug SmartDiffType
      au!
      au VimEnter * :if &diff && len(&ft) | call setwinvar(2/winnr(),'&ft',&ft) | elseif &diff | let &ft=getwinvar(2/winnr(),'&ft') | endif
    aug END
    

    Notes:

    • Of the 4 lines above, you only need the au VimEnter line, but it is generally a good practice to put autocommands in some autocommand group with a reset (au!) at the top.
    • Autocommand on VimEnter because otherwise diff or the windows are not properly intialized yet
    • vimdiff might have been triggered with the old file on the right or the left of the split, so we consider both cases.
    • The 2/winnr() is a math trick to flip between 1 and 2 (2/2 = 1, 2/1=2)