By Joe ‘Zonker’ Brockmeier on June 11, 2007 (9:01:00 PM)
By itself, Vim is one of the best editors for shell scripting. With a little tweaking, however, you can turn Vim into a full-fledged IDE for writing scripts. You could do it yourself, or you can just install Fritz Mehner’s Bash Support plugin.
To install Bash Support, download the zip archive, copy it to your ~/.vim directory, and unzip the archive. You’ll also want to edit your ~/.vimrc to include a few personal details; open the file and add these three lines:let g:BASH_AuthorName = 'Your Name' let g:BASH_Email = 'firstname.lastname@example.org' let g:BASH_Company = 'Company Name'
These variables will be used to fill in some headers for your projects, as we’ll see below.
The Bash Support plugin works in the Vim GUI (gVim) and text mode Vim. It’s a little easier to use in the GUI, and Bash Support doesn’t implement most of its menu functions in Vim’s text mode, so you might want to stick with gVim when scripting.
When Bash Support is installed, gVim will include a new menu, appropriately titled Bash. This puts all of the Bash Support functions right at your fingertips (or mouse button, if you prefer). Let’s walk through some of the features, and see how Bash Support can make Bash scripting a breeze.
Header and comments
If you believe in using extensive comments in your scripts, and I hope you are, you’ll really enjoy using Bash Support. Bash Support provides a number of functions that make it easy to add comments to your bash scripts and programs automatically or with just a mouse click or a few keystrokes.
When you start a non-trivial script that will be used and maintained by others, it’s a good idea to include a header with basic information — the name of the script, usage, description, notes, author information, copyright, and any other info that might be useful to the next person who has to maintain the script. Bash Support makes it a breeze to provide this information. Go to Bash -> Comments -> File Header, and gVim will insert a header like this in your script:#!/bin/bash #=============================================================================== # # FILE: test.sh # # USAGE: ./test.sh # # DESCRIPTION: # # OPTIONS: --- # REQUIREMENTS: --- # BUGS: --- # NOTES: --- # AUTHOR: Joe Brockmeier, email@example.com # COMPANY: Dissociated Press # VERSION: 1.0 # CREATED: 05/25/2007 10:31:01 PM MDT # REVISION: --- #===============================================================================
You’ll need to fill in some of the information, but Bash Support grabs the author, company name, and email address from your ~/.vimrc, and fills in the file name and created date automatically. To make life even easier, if you start Vim or gVim with a new file that ends with an .sh extension, it will insert the header automatically.
As you’re writing your script, you might want to add comment blocks for your functions as well. To do this, go to Bash -> Comment -> Function Description to insert a block of text like this:#=== FUNCTION ================================================================ # NAME: # DESCRIPTION: # PARAMETERS: # RETURNS: #===============================================================================
Just fill in the relevant information and carry on coding.
The Comment menu allows you to insert other types of comments, insert the current date and time, and turn selected code into a comment, and vice versa.