How do I save a file in vi? How do I make changes in vi? How do I move around in the vi editor?

A: Read detailed explanation below.

The best way to learn vi (as with anything else) is to play around with it, make mistakes and learn from experience. Admittedly, it is not a very intuitive piece of software for a new user, but it is extremely useful and is included in almost every UNIX flavor. In order to help you learn vi in this way, this solution will present the vi functions in chart form and attempt to explain some of the concepts. Here’s what we’ll talk about:

  1. How to open a file.
  2. How to move around within a file.
  3. How to save & quit.
  4. How to quit without saving.
  5. How to search for a string of text.


1) How to open a file.

You can open vi in two ways:

1. By typing:

vi {filename}

for example:

vi messages

2. By typing:


In the first case above, if the filename exists in the current directory*, it will open that file. If the file does not exist, it will open a blank file with that name. NOTE: The file is not yet written to disk, so if you do not save it, the file will not live on after you close vi.

*What do I mean by “in the current directory”? Well, if you want to edit a file called info in your /data directory, but you type vi info from the /usr directory, vi will not open the file you were expecting. Instead it will do what is stated above when you type a filename and the file does not exist.

In the second case mentioned above, vi just opens with an untitled blank file.

2) How to move around in a file.

Before you start using vi, you need to know that there are two modes in vi:

a) COMMAND MODE : In this mode, characters that you type are are treated as commands.
b) INPUT MODE: In this mode, characters that you type appear on screen as text input.

By default, when you open vi, you will be in COMMAND MODE.


Note that vi is case-sensitive and that letters written in lower-case must be typed as lower case,
while letters written in upper case must be typed as such.
action				key	detailed explanation
move cursor down		j
move cursor up			k
move cursor left		h
move cursor right		l
[the arrow keys also work, but they are not nearly as geeky as using the keys above ;) ]
delete current character	x	deletes the character at the current cursor position
delete current line		dd	deletes the line the cursor is currently at and shifts everything up one line
undo last change		u	self explanatory
delete to end of line		D	deletes from the current cursor position to the end of the line
move to next word		w	moves the cursor to the beginning of the next word on the line
one screen forward		^f	moves the cursor one screen forward
one screen backward		^b	moves the cursor one screen backward
move to beginning of file	1G	moves the cursor to the first character in the file
move to end of file		G	moves the cursor to the beginning of the last line in the file

To get into input mode, you must press one of the following keys:

insert mode			i
append mode			a
replace single			r
replace multiple		R

Insert mode is pretty self explanatory: it’s like pressing the “INSERT KEY” and typing into a Word Document. It simply pushes all the text to the right of the cursor as you type.

Append is the opposite of insert mode. It just overwrites as you type.

Replace Single will replace a single letter with the next key you hit after having hit “r”.

Replace Multiple will replace all the letters you type up until you hit [ESC] to return to COMMAND MODE.

NOTE: If you use the arrow keys to move around when you are input mode, you will notice strange things happening. It will not react as you expect it to and may output odd characters to the screen. You must hit [ESC] to get back into COMMAND MODE before you can use the arrows to move around in the file again.

3) How to save and quit.

Once you’ve finished editing a file you are going to need to save it and exit vi. Well, unlike Windows Notepad, there is no “X” in the top right hand corner – and there is no FILE > SAVE AS… for you to save your file. So how do you do it? Hit the following key sequence:

[ESC] (the escape key)


So what’s happening here? Hitting they [ESC] key ensures you go back to COMMAND MODE if you’re not already there — if you’re already there, it doesn’t hurt anything. The : tells vi you’re about to give it a command to perform on the file. The w tells it that you want to write the file to disk (i.e. save it) and the q tells it that you want to quit after saving.

4) How to quit without saving.

Say you made some mistakes and you don’t want to save the file you’re working on. You want to basically throw out all the changes you’ve been making during this session. Simple — if you use this key sequence:



The q tells vi to quit and the ! tells it to not to complain about your changes being lost and to just do what you say. Don’t you wish you could just ! some of your co-workers sometimes??

5) How to search for a string of text.

One thing I almost forgot — another common thing you may want to do is to search for a string of text. Well, this is quite easy to do in vi. While you Microsoft Word or Notepad may require you to click EDIT > FIND, it’s actually easier in vi, because it only requires one keystroke!

Closing comments
vi is much more complex and full-featured than I could ever hope to cover in an article this short. If you’re feeling truly geeky and want to learn more about vi and it’s more advanced features, here are a few places for you to check out:

PS: Don’t forget to stop by for all your Sun hardware and consulting needs.

Comments are closed.