Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

vi intro -- the cheat sheet method

Daniel Robbins (drobbins@gentoo.org), President/CEO, Gentoo Technologies, Inc.
Daniel Robbins resides in Albuquerque, New Mexico. He is the President/CEO of Gentoo Technologies, Inc., the Chief Architect of the Gentoo Project and a contributing author of several books published by MacMillan: Caldera OpenLinux Unleashed, SuSE Linux Unleashed, and Samba Unleashed. Daniel has been involved with computers in some fashion since the second grade, when he was first exposed to the Logo programming language as well as a potentially dangerous dose of Pac Man. This probably explains why he has since served as a Lead Graphic Artist at SONY Electronic Publishing/Psygnosis. Daniel enjoys spending time with his wife, Mary, and his new baby daughter, Hadassah. You can contact him at drobbins@gentoo.org.

Summary:  This tutorial shows how to use vi, a powerful visual editor. Using an accelerated cheat sheet method, this tutorial aims to make you a proficient vi user without requiring a huge time commitment. You will quickly learn how to move around, edit text, use insert mode, copy and paste text, and use important vim extensions like visual mode and multi-window editing.

Date:  15 Nov 2006 (Published 19 Dec 2000)
Level:  Introductory PDF:  A4 and Letter (234 KB | 13 pages)Get Adobe® Reader®

Activity:  29262 views
Comments:  

Productivity features

So far, so good...

So far, we've covered how to move, save and quit, perform simple edits and deletions, and use insert mode. With everything listed on the cheat sheet so far, you should be able to use vi to perform almost any task.

However, vi also has many more powerful commands. In this section, you'll learn how to cut, copy and paste, search and replace, and use autoindent features. These commands will help make vi more fun and productive.

Visual mode

The best way to cut and paste is to use visual mode, a special mode that has been added to modern versions of vi, like vim and elvis. You can think of visual mode as a "highlight text" mode. Once the text is highlighted, it can be copied or deleted, and then pasted.

You enter visual mode by hitting v. You will see the word VISUAL at the lower left of your editor as shown in Figure 3. Then, by moving the cursor using movement commands (typically the arrow keys), you'll be able to highlight a region of text. If your Window manager doesn't take over the mouse keys for you, you can also highlight by simply dragging the left mouse button over a particular region.


Figure 3. Using vi's visual mode
Using vi's visual mode

Once highlighted, we are ready to cut or copy the text. If you're copying the text, hit y (which stands for "yank"). If you're cutting the text, hit d. You'll be placed back in command mode. Now, move to the position where you'd like to insert the cut or copied text, and hit P to insert after the cursor, or p to insert before the cursor. Voila, the cut/copy and paste is complete! Test out several copy/cut and paste operations before continuing.


Replacing text

To replace patterns of text, we use ex mode. If you'd like to replace the first pattern that appears on the current line, type :s/regexp/replacement/ and hit Enter, where regexp is the pattern you'd like to match and replacement is the replacement string. To replace all matches on the current line, type :s/regexp/replacement/g and hit Enter. To replace every occurrence of this pattern in your file (normally what you want), type :%s/regexp/replacement/g. If you'd like to do a global replace, but have vi prompt you for each change, type :%s/regexp/replacement/gc (stands for "confirm") and hit Enter.


Indentation

vi supports autoindentation, for when you are editing source code. Most modern versions of vi (like vim) will auto-enable autoindent mode when you are editing a source file (like a .c file, for example). When autoindent is enabled, you can use ^d (control-d) to move one indent level to the left, and ^t (control-t) to move one indent level to the right. If autoindent wasn't enabled automatically, you can manually enable it by typing in the ex command :set autoindent. You can also tell vi to set the tab size to your favorite setting by using the :set tabstop command; :set tabstop=4 is quite popular.


Your final cheat sheet

Well, we've reached the end of the vi tutorial! After adding all the advanced editing commands to your cheat sheet, it should look like this:


Wrap up

Keep your cheat sheet handy, and begin using vi to edit files and compose emails. Refer to the cheat sheet when needed; you'll find that within the week, you'll have nearly all the commands memorized and your vi productivity will shoot through the roof!

5 of 7 | Previous | Next

Comments



Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Linux
ArticleID=89527
TutorialTitle=vi intro -- the cheat sheet method
publish-date=11152006
author1-email=drobbins@gentoo.org
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.