How to Write a Perl Script in VI
Vi is an old fashioned text editor originally developed in 1976. Despite it's age, it remains popular with many programmers and Linux fans for the powerful text manipulation tools built into it. Despite this power, many new users are driven away from it by its steep learning curve compared to other text editors. To learn Vi, it is best to dive right in and start working by writing a simple Perl script in the Vi text editor.
Instructions
-
-
1
Start the Vi editor. In Windows, you should have an option in your Start menu for it. Linux users simply need to type ": vi" (or "vim") into their consoles. Mac OS X users need to open "Applications/Utilities/Terminal" and type "vim." You will be presented with the Vi menu. The first important thing to note is that Vi has two modes: command mode (also called normal mode) and insert mode, and it's important to remember what mode you are in at all times. Later versions of Vi will include a notice at the bottom of the screen to inform you when you are in "Insert" mode. Whenever you start Vi, you will always begin in command mode.
-
2
Switch to Insert mode. Eight ways are available to do this depending on how you want to insert the text. The simplest is simply by pressing the "i" key. This will cause you to insert text at the current cursor position, exactly as you would expect in any other text editor.
-
-
3
Type the following Perl script as you normally would in a text editor:
use Net::FTP
$ftpClient = Net::FTP->new("ftpserver.com")
$ftpClient->login("user", "pass")
$ftpClient->quit
You may notice a problem: the writer of this script, too used to working in Ruby, left off the semicolons at the end of each line. This needs to be fixed.
-
4
Hit "Esc" to exit Insert mode and return to command mode. Press "A" to go to the end of the current line and enter Insert mode. Press ";" to insert the semicolon. Now hit "Esc" again to exit command mode.
-
5
Press the "k" key (or the up arrow) to move up to the previous line. Press "." and this powerful command causes it to repeat the most recent series of actions (in this case, "A" command and type ";".) So, no matter where your cursor ends up on the line, pressing "." will take care of everything. Now you can use "k" and "." to quickly fill the code with the missing semicolons.
-
6
Save your work by typing ":w perl.pl." Quit by typing ":q."
-
1
Tips & Warnings
Pressing capital "I," in command mode will cause the cursor to automatically jump to the beginning of the current paragraph and allow you to insert text from there. Pressing "A" will take you to the end of the paragraph to type, and pressing "O" or "o" will insert a new line above or below the current paragraph.
It is impossible to cover all the features of Vi in a short tutorial. Check out the handy Vi cheat sheet in References.
Always remember what mode you are in: typing commands while in "Insert" mode will cause them to appear in your document. Typing text while in command mode will result in the commands associated with those keystrokes being executed with potentially disastrous results. You can always use the command "u" to undo commands.