15 tips for a more pleasant Vi experience Contributed By Mellonfire Takeaway

advertisement
15 tips for a more pleasant Vi
experience
Version 1.0
March 16, 2005
Contributed By Mellonfire
Takeaway
This download introduces you to some of the features that have given Vi its reputation as a powerful and flexible
text editor. The 15 tips outlined will make your Vi experience more useful and by consequence more enjoyable.
Table of Contents
A MORE PLEASANT VI..........................................................................................................................................................2
1. USE A SPLIT WINDOW TO EASILY NAVIGATE LARGE FILES ...................................................................................................2
2. USE VISUAL MODE TO SELECT AND COPY BLOCKS OF TEXT ...............................................................................................2
3. USE UNDO AND REDO TO CORRECT MISTAKES OR REPEAT COMMANDS ............................................................................2
4. AUTOMATICALLY COMPLETE INSERTED FILE NAMES ...........................................................................................................2
5. EDIT MULTIPLE FILES SIMULTANEOUSLY .............................................................................................................................2
6. USE TEXT ABBREVIATIONS TO INSERT FREQUENTLY-USED TEXT BLOCKS .........................................................................2
7. CREATE BACKUPS OF YOUR WORK .....................................................................................................................................2
8. HIGHLIGHT PROGRAM CODE ................................................................................................................................................2
9. RECORD KEYSTROKES AND PLAY THEM BACK ....................................................................................................................3
10. USE THE RULER TO KEEP TRACK OF YOUR POSITION .......................................................................................................3
11. QUICKLY CALCULATE THE TOTAL NUMBER OF LINES OR WORDS IN A FILE .......................................................................3
12. SEARCH FOR A WORD WITHIN A FILE .................................................................................................................................3
13. PIPE THE OUTPUT OF EXTERNAL COMMANDS INTO A FILE ................................................................................................3
14. ENCRYPT A FILE'S CONTENTS ...........................................................................................................................................3
15. GET CONTEXT-SENSITIVE HELP ........................................................................................................................................3
TECHREPUBLIC ......................................................................................................................................................................4
Additional resources .........................................................................................................................................................4
Version history...................................................................................................................................................................4
Tell us what you think .......................................................................................................................................................4
Page 1
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.
15 tips for a more pleasant Vi experience
A more pleasant Vi
It's hard to think of an application that inspires such extremes of emotion as Vi, the default text editor on most
UNIX systems. Experienced UNIX users and minimalists gibber in delight about its terse interface and powerful
command set; novice users find the same features unfriendly, cryptic and hard to learn.
The truth of the matter lies some where between these two opposing poles. Vi is, certainly, a hard editor to learn
and its single-character commands do take a little getting used to. Once you've figured out the basics though, it's
hard to dispute the application's power and flexibility. The purpose of this document is to introduce you to some of
these features and make your Vi experience more enjoyable. But beware—adding these to your repertoire will
instantly double your geek quotient!
The tips in this tutorial apply to Vim (Vi IMproved) v6.3, freely available from vim.org. All commands are to be
typed in Normal Mode unless otherwise specified.
1. Use a split window to easily navigate large files
When editing a file, type the command: split to split your screen and produce a multi-paned view of the file. You
can navigate each window independently, and use the <Ctrl>-w w key command to switch between them.
Changes made to the file in any one window pane are immediately reflected in the others.
2. Use visual mode to select and copy blocks of text
Type <Shift>-v to shift the editor to Visual Mode. In this mode, you can visually select blocks of text using the
cursor keys, as you would do with a mouse. To cut, copy or paste a selected text block, type: x, y or p,
respectively.
3. Use undo and redo to correct mistakes or repeat commands
Type u to undo your last command, and a period (.) to redo it.
4. Automatically complete inserted file names
If you need to quickly insert a file name, switch to Insert Mode and type the first few letters of the name. Then,
type the key combination <Ctrl-x> <Ctrl-f>. The editor will scan the current directory for a match and complete the
file name for you, if found. If multiple matches exist, type <Ctrl-f> to cycle through them.
5. Edit multiple files simultaneously
Type the command: new filename to open a new window with the named file. You can switch between windows
with the <Ctrl-w> w command and save all open files by typing the command: wa.
6. Use text abbreviations to insert frequently-used text blocks
Define abbreviations for commonly-used text blocks, like your name and address, by typing the command: ab
abbreviation complete-string. For example, the command: ab myemail joe@domain.com would automatically
replace the string "myemail" with the string "joe@domain.com".
7. Create backups of your work
Type the command: set backup to have the editor create a backup copy of a file before writing any changes to it.
This built-in backup capability can help you easily revert to an earlier version of your work. Backups are created in
the same directory as the original file.
8. Highlight program code
To activate syntax highlighting on a script or program file, type the command: syntax enable. The editor will use
the file extension to determine the programming language and load an appropriate color map. To turn syntax
highlighting off, type the command: syntax off.
Page 2
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.
15 tips for a more pleasant Vi experience
9. Record keystrokes and play them back
ViM lets you record keystrokes and play them back, similar to a macro recorder. To start recording, type q#
(where # is a number), followed by the keystrokes you wish to record. Stop recording by switching to Normal
Mode and typing q. You can replay your recording by typing @#, where # is the record number.
10. Use the ruler to keep track of your position
ViM comes with a ruler designed to help you keep track of your current position in the file. The ruler provides
information on your current row and column position in the file, together with a percentage indicator showing how
deep you are in the file. To activate the ruler, type the command: set ruler.
11. Quickly calculate the total number of lines or words in a file
Type g <Ctrl>-g to see statistics on the total number of lines and words in the file.
12. Search for a word within a file
Type the command /search-string to have the editor scan the file forwards from the current cursor position for a
match. Type n to cycle forward through the matches and b to cycle backwards.
13. Pipe the output of external commands into a file
Type the command: read ! external-command to execute an external shell command and pipe its output into the
file. One of the most common applications of this tip is to insert the current date and time into the file, by typing
the command: read !date.
14. Encrypt a file's contents
Start ViM with the -x parameter, and provide a secret key to encrypt the file. The file will now be saved in
encrypted form, unreadable to anyone (except users who know the secret key and can decrypt it).
15. Get context-sensitive help
At any time, type: help keyword to obtain context-sensitive help on a particular feature. For example, the
command: help backup produces a page of information on automatic backups.
Page 3
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.
15 tips for a more pleasant Vi experience
TechRepublic
Additional resources
•
Vi for programmers, part 1 (Article)
•
TechRepublic VI cheat sheets (Download)
•
Use vi to edit files in a UNIX environment (Article)
Version history
Version: 1.0
Published: March 16, 2005
Tell us what you think
TechRepublic downloads are designed to help you get your job done as painlessly and effectively as possible.
Because we're continually looking for ways to improve the usefulness of these tools, we need your feedback.
Please take a minute to drop us a line and tell us how well this download worked for you and offer your suggestions
for improvement.
Thanks!
—The TechRepublic Downloads Team
Page 4
Copyright ©2005 CNET Networks, Inc. All rights reserved.
To see more downloads and get your free TechRepublic membership, please visit http://techrepublic.com.com/2001-6240-0.html.
Download