Wikia

Vim Tips Wiki

Changes: Forcing Syntax Coloring for files with odd extensions

Edit

Back to page

(Adjust previous/next navigation + minor manual clean)
(Undo revision 37643 by 77.242.202.228 (talk) "setfiletype" is the correct command to set a filetype if none has been set yet, see :help :setfiletype)
 
(7 intermediate revisions by 6 users not shown)
Line 75: Line 75:
   
 
----
 
----
Include this autocommand to .vimrc:
+
Include this autocommand to .vimrc, if you do NOT want to override a previous filetype detection, but only set a filetype if the filetype was not detected at all:
   
 
" associate *.foo with php filetype
 
" associate *.foo with php filetype
 
au BufRead,BufNewFile *.foo setfiletype php
 
au BufRead,BufNewFile *.foo setfiletype php
   
----
+
To override any filetype which was already detected, use this instead (note the 'set filetype=' syntax):
the line to add to your .vimrc in the previous comment should be :
 
 
au BufRead,BufNewFile *.module set filetype=php
 
au BufRead,BufNewFile *.module set filetype=php
   
  +
e.g.: Vim 7.3 detects example.pc as filetype 'proc' but has no syntax highlighting for Pro C. Override it so that it highlights as C.
  +
au BufRead,BufNewFile *.pc set filetype=c
 
----
 
----
  +
For individual files, auto-setting is very useful (help :auto-setting). If the 'modeline' option is on, you can simply add a comment to the beginning or end of the file:
  +
# vi:syntax=diff

Latest revision as of 15:30, June 30, 2014

Tip 425 Printable Monobook Previous Next

created 2003 · complexity basic · author zzapper · version 6.0


If you are editing a file containing PHP script (for example) but the file doesn't have the extension .php, you can force the desired syntax coloring with

:set syntax=php

similarly

:set syntax=perl
:set syntax=html

Alternatively if the extension causes an undesired coloring, switch off coloring with

:set syntax=off

look in the directory */vim/vim61/ftplugin / for supported languages (ftplugin == FileType Plugin).

CommentsEdit

You can also let vim determine the filetype by examining the file contents with getline(), see :help new-filetype-scripts


I got these detections by content in my $VIM/vimfiles/ftplugin/filetype.vim:

" IAR option files for compiler/linker
if getline(1) =~ '-!'
  set ft=xcl
  " c/cpp in any file containing c/cpp comment in line 1 or 2
elseif ((getline(1) =~ '\/\*') || (getline(1)=~ '\/\/') || (getline(2) =~ '\/\*') || (getline(2)=~ '\/\/'))
  set ft=c
  " dosbatch in any file containing comment in line 1, 2 or 3
elseif ((getline(1) =~ '^rem') || (getline(2)=~ '^rem') || (getline(3)=~ '^rem'))
  set ft=dosbatch
endif

This already worked in vim5.x using script file, see ':h myscriptsfile'


You mean vim/vim61/syntax. The ftplugin files contain such things as useful mappings for editing certain filetypes, not syntax colouring.


Something that is also very useful for PHP programmers: Sometimes, when you are at a particular place in a file, the Vim buffer "forgets" what the highlighting should be, PHP or HTML. First, try refreshing the window using Ctrl-L. If this does not work, then try changing php_minlines. This can be done by the following line in your .vimrc file.

let php_minlines=500

The default is 100 lines.


fun! SetSyn(name)

can be found in synmenu.vim in ($VIM/vim62 on win2k); and after some initial handling, runs:

exe "set syn=" . name

On Win2k, the current all-in-one installer puts the default filtype.vim in $VIM/vim62. My augmentation of filetype.vim (with the same name) is under $VIM/vimfiles. Check out

to understand the search order. I have a number of syntax highlighting files under $VIM/vimfiles/syntax, only a couple of ftplugins under $VIM/vimfiles/ftplugin (typically for key mapings), and one indent file under $VIM/vimfiles/indent. But, any special handling for filetype detection goes in the filetype.vim.


Using ':setf perl' also works well, and is fewer characters.


Include this autocommand to .vimrc, if you do NOT want to override a previous filetype detection, but only set a filetype if the filetype was not detected at all:

" associate *.foo with php filetype
au BufRead,BufNewFile *.foo setfiletype php

To override any filetype which was already detected, use this instead (note the 'set filetype=' syntax):

au BufRead,BufNewFile *.module set filetype=php

e.g.: Vim 7.3 detects example.pc as filetype 'proc' but has no syntax highlighting for Pro C. Override it so that it highlights as C.

 au BufRead,BufNewFile *.pc set filetype=c

For individual files, auto-setting is very useful (help :auto-setting). If the 'modeline' option is on, you can simply add a comment to the beginning or end of the file:

# vi:syntax=diff

Around Wikia's network

Random Wiki