Lazy person's guide to vi
From DIT NetSoc
Contents |
What is vi?
vi (pronounced vee-eye, not vie) is a modal console text editor which you can expect to find on every GNU/Linux, Unix and Unix-like operating system. For this reason and others it is very useful to be able to use it.
There are many different versions of vi including vim, elvis, nvi, vile, vigor, bvi and more... There's even an implementation of vi in the minimalist embedded shell, busybox. This tutorial will concentrate (but not too much) on vim as that's what's installed on calculon and is most common on GNU/Linux systems. It will just give you an overview - the basics needed to get by. There are links at the bottom of the page to further reading.
Why's this tutorial so good for lazy people?
It's not. I'm the lazy person referred to in the title. Since I haven't learned how to use the editor in my years of Unix(-like) OS use I thought writing this guide might force me to learn it. It might just work.
A note on laziness
In some circles (esp. Perl ones) Laziness is considered a virtue which leads to the improvement of the computer programmer's life. Laziness is what prompts you to spend 3 hours working on a set of scripts to do that daily 15 minute job that's been irritating you. This is a separate but related idea to the concept that it's a lot more interesting to spend 2 hours coding an automated solution for a task than spending 2 hours doing the task itself.
vi modes
vi has two modes:
Command mode
This is the default mode in which vi starts up. From here you can move around the text, copy/paste blocks, delete characters/lines/blocks and more.
You should be able to move around the text with the arrow keys. Traditionally vi uses h (left), j (down), k (up) and l (right) to move the cursor so if the arrow keys don't work try using them. Other movement keys include 0 (beginning of line), Ctrl-F (one screen forward), Ctrl-B (one screen backward), 1G (start of file) and G (end of file).
Insert mode
From command mode you can press 'i' to enter insert mode. From this mode you can insert new text. You can see if you're in insert mode by looking at the bottom of the screen - if you see "-- INSERT --" then you're ready to insert new text.
Press Esc to return to Command mode.
Issuing commands
The basic format of vi commands is:
[count] command [where]
This means that a command can be performed on a number of characters or lines by typing that number before issuing the command, eg; The 'x' command deletes a single character. If you type 10x in command mode that will delete 10 characters from the cursor position. Some commands accept a where parameter but generally commands operate from the cursor position.
There are also colon (ex) commands. These commands are actually part of the ex line editor which vi was built upon. They are preceded with a colon character, eg; in command mode type ':' - you'll see a colon appear at the bottom of the screen. Now type q to try to exit vi. If you have edited your text since the last write (save) you'll see an error such as:
E37: No write since last change (add ! to override)
To quit without writing these changes use ':q!' or to quit and write the changes use ':wq'. Unlike vi commands these commands aren't executed until you hit return.
Basic command summary
Editing commands
Note: Case in important in the following commands.
'i' - enter insert mode
'A' - append - enters insert mode, moving the cursor to the end of the current line
'O' - insert line - enters insert mode, editing a new line above the current line
'x' - delete character under cursor
'r' - replace character under cursor
'cw' - change word - replace word under cursor
'nd' - delete n lines
'dd' - delete current line
'ny' - yank (copy) n lines
'yy' - yank (copy) current line
'p' - paste below current line
'P' - paste above current line
'J' - join lines - joins the current line with the one underneath
'u' - undo!
'U' - undo all changes to the current line
ex commands
':w' - write files
':q' - quit
':q!' - quit without saving
':wq' - quit and write files
':f' - display information on the current file
Tutor
vim has a built in tutor. Simply run it from your shell to use it:
you@calculon:~$ vimtutor
Links
- A vim commands "cheat sheet" - A summary of the commands available in vim
- vi lover's home page - A page for vi fans. Includes vi version details and links to advanced tutorials
