A Quick Guide to vi
Pages: 1, 2
Saving a File
If you start
vi by specifying a filename, you can simply save the file by pressing
:w and return. Note that pressing "
:" when in Command mode will bring you to the bottom of the screen. This is where you can issue further commands.
If you start
vi without any parameters, you need to provide a filename:
~ ~ :w newfile.txt
You can also use this method to save the file into another file. Note that if the filename you specify already exists,
vi will not overwrite the existing file. Instead, use
:w! to overwrite the existing file.
Inserting a File
There may be times where you need to add the content of another file into your current file. In this case, you can use
:r, like this:
~ ~ :r anotherfile.txt
Simply first position the cursor at the line where you want to insert the file and then press
To exit from
vi, use the
:q command. If the file has been modified and you have not saved the changes,
vi will not allow you to quit. To quit without saving, use
Opening a File
If you want to edit another file while you are still in
vi, you can use
:e to open another file for editing:
~ ~ :e textfile.txt
Copying, Cutting, and Pasting Text
To copy a block of text, you can use the
y (for yank) command. To yank the whole line, press
To copy a word, use
To copy from the cursor to the end of the line, use
To paste yanked text, use the
p (for put) command.
Continuing from the previous
Note that for cut operations, you can use the
d command (see "Deleting Text") to cut a block of text and
p to paste the block of text.
Undoing and Redoing
You can undo your action by pressing
u. Note that you can only undo the last action. To redo the same action, use the period (
Changing and Replacing Text
You can change the text in your file by using the
c command. For example,
cw allows you to change the entire word:
To change the last three words, use
To change the words starting from the current cursor position to the end of the line, use
To change the words starting from the beginning of the line to the current cursor position, use
c0 (numeric zero).
To replace a single character, position the cursor at the character you want to replace and type
r, followed by a character.
Searching for Text
To search for a particular piece of text in
vi, use the
/ command followed by the text for which to search. For example,
/in will look for the first occurrence of the word "in". To repeat the search, press
/ and then Enter.
To replace all occurrence of a word in a document, you can use the
s command. For example,
:s/in/on will replace the first occurrence of the word "in" with the word "on".
:s/in/on/g will replace all occurrences of the word "in" with the word "on" on the current line.
:1,45s/in/on/g will replace all occurrences of the word "in" with the word "on" from line 1 to line 45.
:%s/in/on/g will replace all occurrences of the word "in" with the word "on" in the entire file.
Jumping to a Line
If you are editing a large file, you can jump directly to a line by typing the line number. For example,
:4 will position the cursor directly on line four. This is useful when you are debugging a program and the compiler has indicated an error on a particular line.
I hope you find this quick tutorial to
vi a useful one. Remember, the more you use
vi the more you will enjoy it. Have fun!
Wei-Meng Lee (Microsoft MVP) http://weimenglee.blogspot.com is a technologist and founder of Developer Learning Solutions http://www.developerlearningsolutions.com, a technology company specializing in hands-on training on the latest Microsoft technologies.
Return to the Mac DevCenter.
if you want color-coded syntax hiliting,
2003-02-26 05:37:19 anonymous2 [View]
Re: or *don't* use pico
2003-02-26 05:20:01 anonymous2 [View]
ZZ save & quit
2003-02-25 13:38:18 anonymous2 [View]
link to the below
2003-02-24 13:32:31 anonymous2 [View]
A good general resource for vi (vim)
2003-02-24 13:28:27 anonymous2 [View]
2003-02-24 12:18:48 anonymous2 [View]
or *don't* use pico
2003-02-23 18:47:31 anonymous2 [View]
Or just use pico...
2003-02-22 21:16:35 anonymous2 [View]
2003-02-22 19:58:30 anonymous2 [View]
gViM and ViM on OS X
2003-02-22 09:48:55 anonymous2 [View]
another handy one
2003-02-21 20:20:33 anonymous2 [View]
2003-02-21 17:22:01 anonymous2 [View]