-- --
USD:11-4 An Introduction to Display Editing with Vi
As you know now if you tried hitting ˆD, this command scrolls down in the file. The D thus stands for down.
Many editor commands are mnemonic and this makes them much easier to remember. For instance the command to
scroll up is ˆU. Many dumb terminals can’t scroll up at all, in which case hitting ˆU clears the screen and refreshes it
with a line which is farther back in the file at the top.
If you want to see more of the file below where you are, you can hit ˆE to expose one more line at the bottom
of the screen, leaving the cursor where it is. The command ˆY (which is hopelessly non-mnemonic, but next to ˆU
on the keyboard) exposes one more line at the top of the screen.
There are other ways to move around in the file; the keys ˆF and ˆB move forward and backward a page, keep-
ing a couple of lines of continuity between screens so that it is possible to read through a file using these rather than
ˆD and ˆU if you wish.
Notice the difference between scrolling and paging. If you are trying to read the text in a file, hitting ˆF to
move forward a page will leave you only a little context to look back at. Scrolling on the other hand leaves more
context, and happens more smoothly. You can continue to read the text as scrolling is taking place.
2.2. Searching, goto, and previous context
Another way to position yourself in the file is by giving the editor a string to search for. Type the character /
followed by a string of characters terminated by CR. The editor will position the cursor at the next occurrence of this
string. Try hitting n to then go to the next occurrence of this string. The character ? will search backwards from
where you are, and is otherwise like /.†
If the search string you give the editor is not present in the file the editor will print a diagnostic on the last line
of the screen, and the cursor will be returned to its initial position.
If you wish the search to match only at the beginning of a line, begin the search string with an ↑. To match
only at the end of a line, end the search string with a $. Thus /↑searchCR will search for the word ‘search’ at the
beginning of a line, and /last$CR searches for the word ‘last’ at the end of a line.*
The command G, when preceded by a number will position the cursor at that line in the file. Thus 1G will
move the cursor to the first line of the file. If you give G no count, then it moves to the end of the file.
If you are near the end of the file, and the last line is not at the bottom of the screen, the editor will place only
the character ‘˜’ on each remaining line. This indicates that the last line in the file is on the screen; that is, the ‘˜’
lines are past the end of the file.
You can find out the state of the file you are editing by typing a ˆG. The editor will show you the name of the
file you are editing, the number of the current line, the number of lines in the buffer, and the percentage of the way
through the buffer which you are. Try doing this now, and remember the number of the line you are on. Give a G
command to get to the end and then another G command to get back where you were.
You can also get back to a previous position by using the command `` (two back quotes). This is often more
convenient than G because it requires no advance preparation. Try giving a G or a search with / or ? and then a `` to
get back to where you were. If you accidentally hit n or any command which moves you far away from a context of
interest, you can quickly get back by hitting ``.
2.3. Moving around on the screen
Now try just moving the cursor around on the screen. If your terminal has arrow keys (4 or 5 keys with arrows
going in each direction) try them and convince yourself that they work. If you don’t hav e working arrow keys, you
can always use h, j, k, and l. Experienced users of vi prefer these keys to arrow keys, because they are usually right
underneath their fingers.
† These searches will normally wrap around the end of the file, and thus find the string even if it is not on a line in the di-
rection you search provided it is anywhere else in the file. You can disable this wraparound in scans by giving the com-
mand :se nowrapscanCR, or more briefly :se nowsCR.
*Actually, the string you give to search for here can be a regular expression in the sense of the editors ex (1) and ed(1). If
you don’t wish to learn about this yet, you can disable this more general facility by doing :se nomagicCR; by putting this
command in EXINIT in your environment, you can have this always be in effect (more about EXINIT later.)