Introduction
Anyone that has spent time at the wheel of any nix machine will have no doubt come across a need to use Vi, or on more recent Linux distributions Vim (Vi Improved). I know folk that hate Vi and prefer Emacs or what not, in fact there has been something of a war going on between Vi and Emacs users over which one is better for a long time.
I won’t get into the fact that Vi is better here, but what I will say is that regardless of your preference, you should know your way around Vi. Why? Because no matter what nix system you are on, you will find Vi. You might find Emacs, or some other editor, but you will always find Vi.
Vi happens to be a very powerful editor, and in the right hands it is an awesome tool to behold. At the very least you need to know how to get around and edit your files thereby getting the job at hand done without mucking up anything. To that end I have put together a reference of sorts, detailing commands, features, tips, tricks and basically anything I can think of to share with you about Vi. Take a look, and spend some time with a great text editor. You will thank yourself that you did one day, especially if you are doing anything with UNIX or Linux. I’ll be adding to and revising this as I find more neat-o stuff to add.
The Many Modes Of Vi
Vi is kind of quirky in that it has difference modes. Unlike graphical editors that have lots of buttons to punch, Vi is made to use on the command line, from a terminal. No clicky pushy here. Also, Vi was originally written back when there were only 83 keys and in some cases not even function keys. This meant that some smart guy figured out that if you have different “modes”, you could maximize your commands and still do it all with your fingertips. No mice need apply. The three modes of Vi are:
- Command mode – This is the default mode when you enter vi. While in command mode, most keys or letters are commands. This includes some combinations of keys as well. One thing that bears mentioning that might be foreign to you is that these commands are executed without the need to press Enter. You hit the key and the command is run. Period.
- Insert mode – If you press i (lowercase letter i, for insert), while in Command mode, you will enter Insert mode. While in insert mode, whatever you type is inserted in the file at the cursor position. To get back to Command mode, simply press Esc (the escape key).
- Line mode – Line mode is kind of like another command mode. If you hit escape to enter command mode (if you aren’t there already) and then press the colon ( : ), you will get a prompt in the lower left corner that is simply a … well, colon. It is here that you can enter line mode commands. You type the command you want to run and then press Enter. There are some basic day to day commands that you need to know, and still others that you can live without, but will make your life easier. So, anywhere in this document that I talk about Line mode commands, I’ll put a colon in front of it so designate Line mode commands from Command mode commands. Whenever you use a Line mode command, you simply press the colon key to enter line mode, then type the command you want and press Enter to execute the command. Note, the search commands / and ? work in much the same way.
The Basics
Edit or create a file. | vi filename |
Write (save) the file and quit. | :wq |
Quit without saving any changes (the exclamation point tells Vi to do it and don’t warn or ask questions) |
:q! |
Write (save) the entire file as the file ‘newfile’, overwriting any existing file with the same name | :w! newfile |
Write only the lines from n to m, into the file newfile, overwriting any existing newfile | :n,m w! newfile |
Getting Around In Vi
One really neat trick in Vi is that many commands take number prefixes, for example 4dd will delete 4 lines, and 6h will move the cursor 6 spaces to the left. Neat, huh?
Move to the left one space (Sometimes the left arrow works, too) |
h |
Move down one line (Sometimes the down arrow works, too) |
j |
Move up one line (Sometimes the up arrow works, too) |
k |
Move to the right one space (Sometimes the right arrow works, too) |
l |
Move to the end of the current line | $ |
Move to the beginning of the current line | ^ |
Move to the beginning of the first word on the next line | Enter |
Move to the end of a file | G |
Move to line n where n is the line number you want to go. You can use :0 to move the beginning of the file | :n |
Move to the beginning of the next word | w |
Move to the end of the next word | e |
Move to the beginning of the previous word | b |
Move up one page | Ctrl-b |
Move down one page | Ctrl-f |
Move to the matching character (, ), [, ], {, or } (Press % with your cursor on one of these characters to move your cursor to the other related character) |
% |
Searching for Text
Search down for string | /string |
Search up for string | ?string |
Repeat the last search from your present position | n |
Inserting and Appending Text
Append starting at the right of the cursor | a |
Append at the end of the current line | A |
Insert starting at the left of the cursor | i |
Insert at the beginning of the current line | I |
Open a new line below the cursor, and then enter insert mode | o |
Open a new line above the cursor, and then enter insert mode | O |
Add the contents of the file newfile starting below the current line | :r newfile |
Editing Files
Delete a single character (Don’t forget, entering 5x will delete 5 characters) |
x |
Delete word (5dw will delete 5 words) |
dw |
Delete an entire line (5dd will delete 5 lines, I think you get the picture) |
dd |
Delete a word and start insert mode | cw |
Change a single character (delete a character and start insert mode) |
s |
Change an entire line (delete a line and start insert mode) |
cc |
Delete from cursor to end of line | D |
Change from cursor to end of line (delete and start insert mode) |
C |
Undo last change | u |
Undo all changes to the current line | U |
Join the current line with line that follows (you can press Enter in insert mode to split the line) |
J |
Transpose two characters (this is two commands, x followed by p) |
xp |
Yank (as in copy) one line into the main buffer | y |
Put what is in the main buffer back before the current line | P |
Put what is in the main buffer back after the current line | p |
Show the current line number | Ctrl-g |
Force a redraw of the entire display | Ctrl-l |
Fork a shell (type Ctrl-d to get back to vi) |
:!sh |
Repeat the last text change command at the current cursor position | . |
Note: dd or any other delete command will save the deleted string in the main buffer. Therefore, a cut and paste can be done with dd and then p rather than copy and paste with y and p. However, make sure you don’t overwrite the buffer with some other yank or delete command before you have a chance to paste.>
Customizing Vi: the .exrc/.vimrc file
There is a file located in your home directory that is a collection of Vi directives and commands. This is the way to customize your experience with Vi. On a system that truky uses vi, the file is .exrc, and for most modern systems that run Vim instead, it is .vimrc. I am sure there are other permutations of this, but this should work as a general guideline. This is probably one of the things I like best about Vi, the ability to customize it so much.
Here are some items that can go into these *rc files. Now, this is just a sample, be sure to read the Vi or Vim man pages for all of the details.
This will show when you are in insert mode | set showmode |
Tell Vi to ignore case when searching | set ic |
Turn off ignore case | set noic |
Turn on line numbering | set number |
Turn line numbering off | set nonumber |
Show what line and column I am on | set ruler |
Indent with two spaces when I hit tab | set shiftwidth=2 |
Allow me to overwrite text that I didn’t just type | set backspace=2 |
Interpret tab as an indent command instead of an insert-a-tab command | set softtabstop=2 |
Expand all tabs to spaces according to the shiftwidth parameter (basically, this converts existing tabs to spaces based on your shiftwidth setting) |
set expandtab |
Pingback: Vi reference | Solarum - Information For Everyone