Saturday, November 21, 2009

VIM Syntax Settings

Recently I started to learn and working on Windows PowerShell scripts. PowerGUI is a nice tool as script IDE or editor. However, when I wanted to write a blog on PowerShell with some example script codes, I have to use VIM or MacVim to convert codes to HTML. By default, my VIM does not have syntax vim file for PowerShell, since it is a new script language. Then I searched for the ps1.vim on web. It does not take much time to find out one at vim.org's syntax library.

This is first time for me to add syntax file to VIM. It was challenge for me and I took about 2 hours in last evening to figure it out. VIM is an excellent editor for programmers. It provides configuration settings for adding syntax files. Basically, the settings are in two different areas. The first one is to add syntax file to a specified directory. Most cases, syntax files can be found on web by many VIM fans and gurus. I don't need to write one even there are detail information about writing syntax files. Taking PowerShell script as example, the syntax file should be ps1.vim.

Add Syntax File

In case of my Mac OS(UNIX system), there are two places I can place the file. One is for all users and another one is a login user. All the syntax files come with VIM are installed in my Mac at /usr/share/vim/vim72/syntax/ folder. I found this location by using the following command in Terminal:


[Home] $ find / -name "html.vim" -print


Since I have never added any syntax file by myself before, there is no syntax files specific for my login name. I have to create the following folder for my syntax files.


[Home] $ mdkdir ~/.vim/syntax
[Home] $ cp ~/Downloads/ps1.vim ~/.vim/syntax/ps1.vim


After that, I thought I should be able to test a ps1 file in VIM with syntax colors. I did not see any syntax colour when I opened a test file: test.ps1. It took a while to figure out a way to manually load or test the syntax file by using the command in vim:


:source ~/.vim/syntax/ps1.vim



The loading process was failed because there are some syntax errors in the ps1.vim file. I think that actually is the line break problems when I downloaded the file from web: some line breaks are actually ^M chars. Then I found another updated ps1.vim from tomsr. I copied the source codes from web page and replaced ps1.vim's whole content. After that, the loading process was OK.

NOTE on September 16, 2010: for Windows XP, the vim.ps1 from VIM syntax library is OK. I have to copy this file to my VIM\vimfiles\syntax folder.

Define File Types

Still I could not see syntax colour. This is because VIM requires another setting to define file types. This second setting is called as *new-filetype*. I choose C option to define my new file type. I copied the following codes to a file at ~/.vim/filetype.vim:

if exists("did_load_filetypes")
  finish
endif
augroup filetypedetect
  au! BufRead,BufNewFile *.ps1    setfiletype ps1
augroup END


Then I restarted my VIM. Finally I can see my test.ps1 file in VIM with syntax colours.



NOTE on September 16, 2010: for Windows XP, the file filetype.vim should be copied to VIM\vimfiles.

0 comments: