Vim Tips Wiki
(Move categories to tip template)
(Clean up the filetype.vim rule since filetype.vim explains the file structure.)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=638
 
|id=638
Line 9: Line 8:
 
|version=5.7
 
|version=5.7
 
|rating=2/2
 
|rating=2/2
|category1=
+
|category1=Filetype
 
|category2=
 
|category2=
 
}}
 
}}
To run Perl scripts under Windows, you can either add the .pl extension to the PATHEXT env variable, or use pl2bat, which comes with ActiveState's Perl and makes a very nice batch file. A quirk of Perl under Win32 is that piping doesn't work with .pl files (as in bar.pl | foo.pl) but works fine with the batch files. It has something to do with how Windows loads files. Anyway, this all works fine, but every time I do any extensive edits to a perl/batch file, I have to set cindent and syntax=perl or it drives me crazy. This tip modifies filetype.vim to check batch files to see if they're really perl scripts in disguise.
+
To run Perl scripts under Windows, you can either add the .pl extension to the PATHEXT environment variable, or use pl2bat, which comes with ActiveState's Perl and makes a very nice batch file. A quirk of Perl under Win32 is that piping doesn't work with .pl files (as in bar.pl | foo.pl) but works fine with the batch files. It has something to do with how Windows loads files. Anyway, this all works fine, but every time I do any extensive edits to a Perl/batch file, I have to set cindent and syntax=perl or it drives me crazy.
   
  +
==Filetype.vim==
First, find these lines:
 
  +
To make one change that will apply to any scripts created by pl2bat, edit your user (~/.vim) or machine ($VIMRUNTIME/vimfiles) version of [[filetype.vim]] to include a .bat analysis rule:
   
 
<pre>
 
<pre>
  +
au BufRead,BufNewFile *.bat if getline(1) =~ '--\*-Perl-\*--' | setf perl | endif
" Batch file for MSDOS (*.cmd is close enough)
 
au BufNewFile,BufRead *.bat,*.cmd,*.sys setf dosbatch
 
 
</pre>
 
</pre>
   
  +
This will cause Vim to scan any .bat files for <code>--*-Perl-*--</code> on the first line, and if it's found it will be treated as a Perl file, otherwise the predefined rules for .bat extensions will run.
Then change them to this:
 
   
  +
See {{help|new-filetype}} (section C).
<pre>
 
" Batch file for MSDOS (*.cmd is close enough)
 
au BufNewFile,BufRead *.bat,*.cmd,*.sys call FTCheck_bat()
 
   
  +
==Modeline==
" Perl scripts converted to bat by pl2bat have a unique string that
 
  +
You can specify the following text as the first line of the converted file (by manual edit or changing the pl2bat generation) to contain the following to not need to edit the filetype definition. However, this method only applies to each file changed in such a manner.
" identifies the file. It should be the first line.
 
fun! FTCheck_bat()
 
if exists("g:filetype_bat")
 
exe "setf " . g:filetype_bat
 
else
 
let l = getline(nextnonblank(1))
 
if l =~ '--\*-Perl-\*--'
 
setf perl
 
else
 
setf dosbatch
 
endif
 
endif
 
endfun
 
</pre>
 
 
That's it! This is very specific to look for the string pl2bat adds to the file, but can be easily modified to your needs.
 
 
==Comments==
 
Add this line at the top of your converted pl2bat script and everything should work automatically:
 
   
 
<pre>
 
<pre>
Line 53: Line 32:
   
 
See {{help|modeline}}.
 
See {{help|modeline}}.
 
----
 

Latest revision as of 02:52, 30 September 2008

Tip 638 Printable Monobook Previous Next

created January 19, 2004 · complexity basic · author David DelGreco · version 5.7


To run Perl scripts under Windows, you can either add the .pl extension to the PATHEXT environment variable, or use pl2bat, which comes with ActiveState's Perl and makes a very nice batch file. A quirk of Perl under Win32 is that piping doesn't work with .pl files (as in bar.pl | foo.pl) but works fine with the batch files. It has something to do with how Windows loads files. Anyway, this all works fine, but every time I do any extensive edits to a Perl/batch file, I have to set cindent and syntax=perl or it drives me crazy.

Filetype.vim[]

To make one change that will apply to any scripts created by pl2bat, edit your user (~/.vim) or machine ($VIMRUNTIME/vimfiles) version of filetype.vim to include a .bat analysis rule:

au BufRead,BufNewFile *.bat 	if getline(1) =~ '--\*-Perl-\*--' | setf perl | endif

This will cause Vim to scan any .bat files for --*-Perl-*-- on the first line, and if it's found it will be treated as a Perl file, otherwise the predefined rules for .bat extensions will run.

See :help new-filetype (section C).

Modeline[]

You can specify the following text as the first line of the converted file (by manual edit or changing the pl2bat generation) to contain the following to not need to edit the filetype definition. However, this method only applies to each file changed in such a manner.

@rem vim: set ft=perl:

See :help modeline.