Category Archives: Uncategorized

Vim Scripting Interfaces for Perl, Python and Ruby

Introduction

Vim is a scriptable editor which includes a built-in scripting language. The problem with the built-in language is that it is yet another language for a programmer to learn and remember. In my opinion it would be more efficient if Vim scripts could be written in a language that the programmer already knows, rather than having to learn Vim’s special language. The built-in scripting language is not hard to learn, as programming languages go. But, in my experience, the more programming language syntaxes one has to remember, the harder it is to remember them all in sufficient detail that one can be productive with them all. So, the goal of this article is to show how Vim can be scripted with the normal programming languages that programmers already know, like Perl, Python, Ruby, etc. Vim supports alternate scripting languages with a feature called “Interfaces.” The following languages are among those that are supported:
  1. Perl
  2. Python
  3. Tcl
  4. Ruby

Getting Help in Vim

You can jump directly to the help page for a particular language interface by typing :help . For example,
  • help perl
  • help python
  • help tcl
  • help ruby

Requirements

There are several requirements that have to be met before Vim will recognize an alternate scripting language.
  1. Support for the language must be selected at compile time. When Vim is built, there are options to either include or exclude language interfaces. The
    :version
    command will show which ones are available in a given instance of Vim.
  2. The dll or shared library for the language must be available at run-time. For example, Perl support requires the perl814.dll to be available in the PATH.
  3. The “word size” of the dll or shared library must match with the Vim executable. In other words, if Vim is a 32-bit executable, then perl814.dll must be a 32-bit dll. You can’t mix 32-bit and 64-bit components.

Setup the Perl Interface in Vim on Windows

Install the Perl Version Required by Vim

You should download Perl from Strawberry Perl. I prefer Strawberry Perl because it is completely open source, whereas ActiveState Perl is partly proprietary. You should choose a “32bit PortableZip edition”. Vim is 32-bit only, so it requires a 32-bit Perl. The PortableZip version will allow you to unzip it where ever you prefer. This is important if you already have a 64-bit Perl as your primary version. I would suggest unzipping it into C:\opt\perl-for-vim. You also have to match the Perl version with your Vim version because the name of the dll that Vim is hardcoded into Vim.
  • For Vim 7.4 get Perl 5.14.x.x – 32-bit PortableZip edition
  • For Vim 7.3 get Perl 5.12.x.x – 32-bit PortableZip edition
Unzip into C:\opt\perl-for-vim. I suggest using 7-Zip rather than Windows' built-in Zip functionality because Windows' Zip/Unzip is very slow. 7-Zip is much faster.

Put Perl Dll in the PATH

If this is the only version of Perl you have installed, you should put it into the system path. If you have another version of Perl, and this installation is just for Vim, you can add it to the PATH inside your _vimrc startup file. Edit C:\Users\[yourid]\_vimrc. Add these lines to the top of the file:
		" This puts the Perl dll in the PATH.
let $PATH = 'C:/opt/perl-for-vim/perl/bin;' . $PATH

Test Vim’s Access to Perl

Add these lines to _vimrc and restart Vim:
		if has('perl')
    perl VIM::Msg("The Perl interface is operational.");
endif

If you see a popup dialog with the above message, then it is working.

Write a Perl Script