How to use vi
The text editor vi evolved from a unix editor program which was designed to work with a single line of text at a time, rather than moving seamlessly through pages of text as modern editors do. Its small size coupled with advanced functionality, and the fact it can be used over a terminal link such as an ssh session means it is ubiquitous on modern unix systems.
Users who are used to modern editors will no doubt find vi frustrating, so here's a few tips on basic usage, along with links to more detailed tutorials.
Moving around the page and actually editing text are two different modes in vi and to edit you need to switch from command mode to edit mode. This is because historically some terminal modes did not support cursor or function keys, or misinterpreted the keycodes they sent, so vi users needed to send commands using the letter keys which in a modern editor would simply be typed into the page text.
The most basic command is the one to start editing text, and have vi stop recognising your keystrokes as commands.
- i - start adding text at the current cursor position
- I - start adding text at the start of the line
- a - start adding text one character after the current cursor position
- A - start adding text at the end of the current line
You might ask what good the 'a' command is if you already have an 'i' command that works almost exactly the same. One reason is that some of the more advanced text-replacement commands leave the cursor at the last character of a word, and often you want to add your text after that character, rather than to insert text just before it.
The 'escape' key brings you out of edit mode and back into command mode.
To save your file you need to type :w in command mode and hit enter. To save it to a different file name (say 'foo.txt') type :w foo.txt
To quit you use the command :q. If you get a warning such as "this file is readonly" but you have rights to override the readonly status use :q! to do so.
You can chain your "write" and "quit" commands together with :wq or :wq!