Tuxdude
Tuxdude

Reputation: 49473

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.

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.

Upvotes: 6

Views: 2098

Answers (2)

Daan Bakker
Daan Bakker

Reputation: 6332

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)

Upvotes: 6

Peter Rincker
Peter Rincker

Reputation: 45107

Assuming that you have open only the 2 splits and the split on the left is your local file you can do the following

:windo let &ft = getwinvar(1, '&ft')

This will set the filetype to the value of the top left most window for all windows.

For more help see:

:h :windo
:h 'ft'
:h getwinvar(

Upvotes: 3

Related Questions