Make Vim as Your Perl IDE Using perl-support.vim Plugin

本文介绍了如何通过Perl-Support Vim插件为Perl编程提供一键式功能,包括自动添加文件头部、自动插入子例程、函数注释、框架注释等,并演示了如何快速安装插件及利用其7大功能特性提高编程效率。
摘要由CSDN通过智能技术生成

Make Vim as Your Perl IDE Using perl-support.vim Plugin

by RAMESH NATARAJAN on JANUARY 5, 2009

This is a guest post written by SathiyaMoorthy.

This article is part of the ongoing Vi / Vim Tips and Tricks Series. As a programmer, you may do lot of repetitive tasks while coding such as:

  • Adding file header
  • Adding function/frame comment
  • Including default code snippet
  • Performing syntax check
  • Reading documentation about a function
  • Converting a code block to comment, and vice versa

The Perl-Support Vim Plugin – Perl-IDE offers easiest way to do all of the above, saving lot of time and keystrokes.

The plugin was written by Fritz Mehner, who explains the purpose of the plugin as: “Write and run Perl-scripts using menus and hot keys”.

This article explains how to install the plugin in 3 easy steps and 7 powerful features of the plugin.

3 Steps to Install the Perl-Support Vim Plugin

 

Step 1: Download Perl-Support Vim Plugin

Download the plugin from vim.org website.

$ cd /usr/src
$ wget http://www.vim.org/scripts/download_script.php?src_id=9701

Step 2: Install the Perl-Support Vim Plugin

$ mkdir ~/.vim
$ cd ~/.vim
$ unzip /usr/src/perl-support.zip

Step 3: Enable the plugin in the ~/.vimrc

Add the following line to the ~/.vimrc to enable the plugin for Vim editor.

$ vim ~/.vimrc
filetype plugin on

 

7 Powerful Features of Perl-Support Vim Plugin

 

Feature 1: Add Automatic Header to *.pl file

When you open a file with the extension .pl it opens the file with header as shown below. This will also place the cursor in the Description field in Insert mode.

$ vim myprogram.pl
#!/usr/bin/perl
#===================================================
#
#         FILE:  myprogram.pl
#
#        USAGE:  ./myprogram.pl
#
#  DESCRIPTION:
#
#      OPTIONS:  ---
# REQUIREMENTS:  ---
#         BUGS:  ---
#        NOTES:  ---
#       AUTHOR:  Dr. Fritz Mehner (mn), mehner@fh-swf.de
#      COMPANY:  FH Südwestfalen, Iserlohn
#      VERSION:  1.0
#      CREATED:  12/23/2008 03:47:07 PM
#     REVISION:  ---
#===================================================

use strict;
use warnings;


To change the default value of the AUTHOR and COMPANY, modify the default value in ~/.vim/perl-support/templates/Templates

$ vim ~/.vim/perl-support/templates/Templates
|AUTHOR|    = SathiyaMoorthy
|AUTHORREF| = sm
|EMAIL|     = test@test.com
|COMPANY|   = mycompany


Now, when you create a new perl file, it will show the modified values for AUTHOR and COMPANY as shown below.

$ vim myprogram.pl
#!/usr/bin/perl
#===================================================
#
#         FILE:  myprogram.pl
#
#        USAGE:  ./myprogram.pl
#
#  DESCRIPTION:
#
#      OPTIONS:  ---
# REQUIREMENTS:  ---
#         BUGS:  ---
#        NOTES:  ---
#       AUTHOR:  SathiyaMoorthy (sm), test@test.com
#      COMPANY:  mycompany
#      VERSION:  1.0
#      CREATED:  12/23/2008 04:09:23 PM
#     REVISION:  ---
#===================================================

use strict;
use warnings;


Note: To add custom fields to the header, modify the ~/.vim/perl-support/templates/file-description.template file and add your own custom field.

Feature 2: Adding Perl Subroutine using /isu

For writing a subroutine, type /isu in normal mode, which will prompt for the subroutine name (as shown in Fig1 below) and inserts the subroutine with default function content (as shown in Fig2 below).

Perl Support Insert Sub-Routine
Fig 1: Prompt User For Sub-Routine Name

 

Perl Support Plugin Insert Sub-Routine
Fig 2: Insert Sub-Routine Automatically to the Perl Code

Feature 3: Insert a Function Header using /cfu

For inserting a function header, type /cfu in normal mode, which will add the comment as shown below.

Perl Support Add Function Comment
Fig 3: Insert Function Comment to the Perl Code

Feature 4: Add a Frame comment using /cfr

To add a frame comment, type /cfr in normal mode, which will give the following formatted comment.

Perl Support Frame Comment
Fig 4: Insert Frame Comment to the Perl Code

Feature 5: Save the file and execute it immediately using /rr

Type /rr in the normal mode, which will save the file and execute it immediately.

Feature 6: Comment a block of code using /cc

To change a entire code block to comment, select the code in visual mode and type /cc.

Feature 7: Insert pre-defined code-snippet to the Perl code using /nr

The plugin comes with few pre-defined code snippets that you can insert into your code. Following are the default code snippets that comes with the plugin.

$ ls ~/.vim/perl-support/codesnippets/
dot.SmallProf                print-data-structure-with-Dumper.pl
free-software-comment        print-hash.pl
inside-out-class.pl          print-hash-sorted.pl
module-interface.pl          process-all-files-in-a-directory-recursively.pl
new.pl                       slurp-file.pl
pod-template-application.pl  SmallProf-variables.pl
pod-template-module.pl


For example, if you want to create a function that will print the hash values, you can re-use it from the existing code snippets. Following is the content of the print-hash.pl pre-defined code snippets.

$ cat ~/.vim/perl-support/codesnippets/print-hash.pl

#----------------------------------------------------------------------
#  subroutine : print_hash
#----------------------------------------------------------------------
sub print_hash {
  my  $hashref  = shift;      # 1. parameter : hash reference
  print "/n";
  while ( my ( $key, $value ) = each %$hashref ) {
    print "'$key'/t=>/t'$value'/n";
  }       # -----  end while  -----
} # ----------  end of subroutine print_hash_sorted  ----------


To insert this into your working perl program, type /nr from the normal mode inside vim, which will prompt “read snippet /home/rnatarajan/.vim/perl-support/codesnippets/”, type print-hash.pl at the end and press enter, which will insert the content of the ~/.vim/perl-support/codesnippets/print-hash.pl to your working file automatically.

Note: You can define your own code snippets and place it under ~/.vim/perl-support/codesnippets/. You can also build your own code snippets from the existing code – select the part of code need to be made as code snippet, press /nw, and give a file-name to it. From next time, type /nr and the file-name to get your custom code snippet.


There are lot of powerful features in the PerlSupport Vim Plugin. Read the documentation for more information. The documentation is located in the following location on your system.

 

Recommended Reading

Vim 101 Hacks, by Ramesh Natarajan. I’m a command-line junkie. So, naturally I’m a huge fan of Vi and Vim editors. Several years back, when I wrote lot of C code on Linux, I used to read all available Vim editor tips and tricks. Based on my Vim editor experience, I’ve written Vim 101 Hacks eBook that contains 101 practical examples on various advanced Vim features that will make you fast and productive in the Vim editor. Even if you’ve been using Vi and Vim Editors for several years and have not read this book, please do yourself a favor and read this book. You’ll be amazed with the capabilities of Vim editor.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值