Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Vim does recognize filetype smartly according to the name of the file and the modeline.

However it cannot deal with this case: I have a file without any special special suffix, say somefile, which is a soft link to a c++ source file, somefile.cpp. Vim seems to only look up the name of somefile and think it is a ordinary text file.

So is it possible to make Vim judge the filetype more reasonably(for instance, choose the filetype of the filename that contains the suffix) or prompt to let user choose if it cannot judge in a more complicated case?

share|improve this question
1  
is this for syntax highlighting? you can manually set syntax highlighting to C using :set syn=c –  Jeff Apr 14 '13 at 3:43
    
@Jeff Yeah, I need the syntax highlight. And I would not bother to manually set syntax when I open a file. I am just seeking a general way to handle this problem. –  Hongxu Chen Apr 14 '13 at 4:10
1  
Only other option I can think of is a tweak of @Jeff 's, map it to a hotkey. As a quicker method, anyway. –  nerdwaller Apr 14 '13 at 4:14
    
@HongxuChen I edited my answer to provide a permanent solution –  Jeff Apr 14 '13 at 5:17
1  
An alternative is the method described in :h new-filetype-scripts. –  romainl Apr 14 '13 at 9:51

1 Answer 1

Quick Fix

If you are opening a file for the first type, type

:set syn=c

or some other filetype in place of c to change the syntax highlighting.

If this is a file you edit often, you can add a modeline to the top of your file.

/* vim:syntax=c filetype=c */

Any comment type should work for other filetypes, e.g.

# vim:syntax=python filetype=python

Permanent Solution

Add this line to your .vimrc file:

au BufNewFile,BufReadPre * if &syntax == '' | silent! execute (':set filetype='.matchstr(resolve(@%),'.[^.]*$')[1:]) | endif

Now, any time you open a file without an extension, it will attempt to see if it is a symbolic link and retrieve the target file. If it is in fact a symbolic link, it will set the syntax to the target file's filetype. Otherwise, it does nothing.

share|improve this answer
1  
Generally it's not necessary to set the 'syntax' option if you set the 'filetype' option. –  Heptite Apr 14 '13 at 5:16
    
@Heptite i figured as much, but provided it just in case there is some exception that i am unaware of –  Jeff Apr 14 '13 at 5:18
    
There is, but it would be very unusual for a user to encounter that in a normal configuration. –  Heptite Apr 14 '13 at 6:47
    
@Jeff Seems that syntax is set to target file name other than the syntax type(in this case, syntax=somefile.cpp, not syntax=cpp)? –  Hongxu Chen Apr 14 '13 at 7:25
    
@HongxuChen yes, and it works just fine. –  Jeff Apr 14 '13 at 7:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.