Vim file type detection

Can Vim determine the type of buffer file with contents, but which does not yet have a name (not yet saved)? :filetype detect not much.

+4
source share
4 answers

Most file types are detected by their file name (and / or location), some (mainly those that have the #! Shebang string) by their contents.

If you do not want to save the buffer yet, but have detection, you can name the buffer through :file name.ext . Then detection through :filetype detect will work.

If you just want the syntax highlighting (and the corresponding file options) quickly, just set the file type for the file via :setf name .

+4
source

You can do it manually.

 :set filetype=EXT 

Please note that EXT is an extension for the desired file type, not necessarily the name of the language itself.

+1
source

As help for vim says, it works in some cases, for example, if you type

 #!/bin/sh 

then it will detect as sh script.

0
source

If the file contains #! in line 1, it is easy.

For instance:

 " finish if the filetype was detected if did_filetype() finish endif " Ruby & Python ftplugins if getline(1) =~ '^#!.*ruby' setfiletype ruby elseif getline(1) =~ '^#!.*python' setfiletype python endif 
0
source

All Articles