Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"
International Medical Corps

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents
Up to --> Editing Files

· Vi
· vi Basics
· Changing Text in vi
· Moving Around in vi
· Searching in vi
· vi Buffers
· vi Magic
· Command Output in vi
· More vi Magic
· vi Odds and Ends
· Configuring vi

Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
Copyright Info
Terms of Use
Privacy Info
Masthead / Impressum
Your Account

Private Messages

News Archive
Submit News
User Articles
Web Links


The Web

Who's Online
There are currently, 86 guest(s) and 0 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here

Linux Tutorial - Editing Files - Vi - vi Basics
  Vi ---- Changing Text in vi  

vi Basics

The uses and benefits of any editor like vi are almost religious. Often, the reasons people choose one editor over another are purely a matter of personal taste. Each offers its own advantages and functionality. Some versions of UNIX provide other editors, such as emacs. However, the nice thing about vi is that every dialect of UNIX has it. You can sit down at any UNIX system and edit a file. For this reason more than any other, I think it is worth learning.

One problem vi has is that can be very intimidating. I know, I didn't like it at first. I frequently get into discussions with people who have spent less than 10 minutes using it and then have ranted about how terrible it was. Often, I then saw them spending hours trying to find a free or relatively cheap add-on so they didn't have to learn vi. The problem with that approach is that if they has spent as much time learning vi as they did trying to find an alternative, they actually could have become quite proficient with vi.

There is more to vi than just its availability on different UNIX systems. To me, vi is magic. Once you get over the initial intimidation, you will see that there is a logical order to the way the commands are laid out and fit together. Things fit together in a pattern that is easy to remember. So, as we get into it, let me tempt you a little.

Among the "magical" things vi can do:

  • Automatically correct words that you misspell often
  • Accept user-created vi commands
  • Insert the output of UNIX commands into the file you are editing
  • Automatically indent each line
  • Shift sets of lines left or right
  • Check for pairs of {}, () and [] (great for programmers)
  • Automatically wrap around at the end of a line
  • Cut and paste between documents

I am not going to mention every single vi command. Instead, I am going to show you a few and how they fit together. At the end of this section, there is a table containing the various commands you can use inside vi. You can then apply the relationships to the commands I don't mention.

To see what is happening when you enter commands, first find a file that you can poke around in. Make a copy of the termcap file (/etc/termcap) in a temporary directory and then edit it (cd /tmp; cp /etc/termcap . ; vi termcap). The termcap file contains a list of the capabilities of various terminals. It is usually quite large and gives you a lot of things to play with in vi.

Before we can jump into the more advanced features of vi, I need to cover some of the basics. Not command basics, but rather some behavioral basics. In vi, there are two modes: command mode and input mode. While you are in command mode, every keystroke is considered part of a command. This is where you normally start when you first invoke vi. The reverse is also true. While in input mode, everything is considered input.

Well, that isn't entirely true and we'll talk about that in a minute. However, just remember that there are these two modes. If you are in command mode, you go into input mode using a command to get you there, such as append or insert (I'll talk about these in a moment). If you want to go from input mode to command mode, press Esc.

When vi starts, it goes into full-screen mode (assuming your terminal is set up correctly) and it essentially clears the screen (see the following image). If we start the command as

vi search

at the bottom of the screen, you see

"search" [New File]

Your cursor is at the top left-hand corner of the screen, and there is a column of tildes (~) down the left side to indicate that these lines are nonexistent.

In the image below we see a vi session started from a terminal window running under X-Windows. This is essentially the same thing you will see when starting vi from any command line.

Image - Main vi window. (interactive)

As with most text editors or word processors, vi gives you the ability to save the file you are editing without stopping the program. To issue the necessary command we first input a colon (:) when in command mode. When then press w (for write) and the press the entry key. This might look like the following figure:

Image - Writing a file in vi.

After you press the enter key, you end up with something like the following image:

Image - Writing a file in vi. (interactive)

If you are editing a file that already existing and try to save it like this, you may get an error message that says the file is read only. You will also get this message, when trying to save a file from "view", which is the "read-only" version of vi. To force the file to be written, you follow the w with an exclamation mark. (:w!)

The ex-mode (or command mode) also allows you to do many other things with the file itself. Among them are

  • :q to quit the file (:q! if the file has been changed and you don't want to save the changes)
  • :wq to write the file and quit
  • :e to edit a new file (or even the same file)
  • :r to read in a new file starting at the current location

 Previous Page
  Back to Top
Table of Contents
Next Page 
Changing Text in vi


Test Your Knowledge

User Comments:

You can only add comments if you are logged in.

Copyright 2002-2009 by James Mohr. Licensed under modified GNU Free Documentation License (Portions of this material originally published by Prentice Hall, Pearson Education, Inc). See here for details. All rights reserved.
Show your Support for the Linux Tutorial

Purchase one of the products from our new online shop. For each product you purchase, the Linux Tutorial gets a portion of the proceeds to help keep us going.



Security Code
Security Code
Type Security Code

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!

Amazon Wish List

Did You Know?
The Linux Tutorial welcomes your suggestions and ideas.


Tell a Friend About Us

Bookmark and Share

Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.09 Seconds