Wikia

Vim Tips Wiki

Changes: Forcing Syntax Coloring for files with odd extensions

Edit

Back to page

(Adjust previous/next navigation + minor manual clean)
(Comments)
Line 78: Line 78:
   
 
" associate *.foo with php filetype
 
" associate *.foo with php filetype
au BufRead,BufNewFile *.foo setfiletype php
+
au BufRead,BufNewFile *.foo set filetype=php
   
 
----
 
----

Revision as of 16:23, November 1, 2013

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).

Comments

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:

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

the line to add to your .vimrc in the previous comment should be :

au BufRead,BufNewFile *.module set filetype=php

Around Wikia's network

Random Wiki