Note | |
---|---|
This section is nearly finished but is still under development. |
Tip | |
---|---|
Only the commands that start with a colon ( In addition, all vi and Vim commands are case-sensitive. |
Vi (pronounced VEE-EYE)[37] “was the first real screen-based editor for Unix systems.”[38] It is present on all Unix and Unix-like operating systems (including Linux). Another frequently used text editor, GNU Emacs (discussed in Section 5.2, “GNU Emacs”), is usually (but not always) installed as well. You can start vi by typing vi or vi file_to_edit
(such as vi lab0.c
) at the prompt.
Vi is closely connected to the even older line-based text editor ex, whose commands begin with a colon (:
). [reference! Also something (very) short about age/origin of vi.] In fact, you can start either editor from the command line and can switch between the two while using them. As can be seen from the tables below, vi users must know a few essential ex commands. However, a thorough coverage of ex is beyond the scope of this guide.
Since the original vi was a closed-source project,[39] developers created their own clones of vi, adding new features along the way. One of the most popular of these clones is Vim (short for vi improved), created by Bram Moolenaar. Vim seems to be the standard version of vi on Linux machines, although other vi clones, such as elvis and vile, exist. However, the rest of this section will focus exclusively on Vim. Although Vim has much in common with the original vi and with the various vi clones, there are differences among them, including incompatibilities between Vim and vi. This guide will not cover those distinctions; anyone who is interested can check the references listed in the further reading section.
Vim is a modal editor that fundamentally has two modes: a normal (or command) mode for entering commands and an insert mode for entering and editing text. By comparison, modeless editors such as Emacs are always in insert mode, so that the characters you type always appear as text on the screen. As listed in Table 5.10, “Other modes in Vim”, Vim also includes a replace mode, which differs from insert mode in that any characters typed will overwrite existing characters in the buffer, as well as three types of visual modes, which allow for selecting text for cutting or copying.
Tip | |
---|---|
You can return to normal mode at any time by pressing the Escape key. |
[TODO - Importance of normal/command mode, operators/commands and motions/text-objects, delete vs. change vs. yank (and put?), etc.]
When vi was invented, the H, J, K, and L keys doubled as arrow keys, since there was no separate set of arrow keys on the keyboard. Thus vi and its clones still use those keys as arrow keys in the format shown in Table 5.1, “The Arrow Keys in Vim” below. Although the now-standard arrow keys work in Vim as expected, you may wish to try using the HJKL keys in their place, as you may find it faster to use them rather than having to reach to the arrow keys to use them.
To type a command like C-r, press the R key while holding down the Ctrl/Control key.
References to further reading on vi and Vim can be found in Section A.4.1, “More on vi and Vim”.
Table 5.2. Basic Vim commands, Part 1
Category | Command | Action |
---|---|---|
Opening, Saving, and Exiting | --- | |
~ |
| Open (or create) file |
~ |
| Save current file |
~ |
| Save current file as filename (will not overwrite) |
~ |
| Save current file as filename (will overwrite if necessary) |
~ | :wq or ZZ | Save file and quit Vim |
~ | :q! or ZQ | Quit Vim without saving |
Getting Help | --- | |
~ |
| Vim help |
~ | vimtutor from command line or :help tutor while in Vim | Vim tutor |
Keys Found in Other Editors | --- | |
~ |
| Move cursor by one character/line |
~ | Delete (but not Backspace, except in insert mode) | Delete one character at a time |
Table 5.3. Basic Vim commands, Part 2
Category | Command | Action |
---|---|---|
Commands on Commands | --- | |
~ |
| Run <command> (for example, ls) in shell |
~ |
| Enter normal mode (also quits any command being typed) |
Undo and Redo | --- | |
~ |
| Undo previously typed command |
~ |
| Undo changes to most recently edited line |
~ |
| Revert file to last saved version |
~ |
| Redo most recently performed undo |
~ |
| Redo most recently typed non-movement command |
Table 5.4. Moving through the buffer in Vim
Command | Action |
---|---|
| Indicates your location in the file and other information |
gg or 1G | Move to start of buffer |
| Move to end of buffer |
| Move to line <number> |
| Move up by one screen |
| Move down by one screen |
Table 5.5. Moving through screens and lines in Vim
Category | Command | Action |
---|---|---|
Moving Within the Screen | --- | |
~ |
| Move to top of screen |
~ |
| Move to middle of screen |
~ |
| Move to bottom of screen |
Moving Along a Line | --- | |
~ |
| Move to start of line |
~ |
| Move to first non-whitespace character of line |
~ |
| Move to end of line |
Moving Word by Word | --- | |
~ |
| Move to start of (previous) word |
~ |
| Same as b, but ignore punctuation |
~ |
| Move to end of (next) word |
~ |
| Same as e, but ignore punctuation |
~ |
| Move to start of next word |
~ |
| Same as w, but ignore punctuation |
Table 5.7. Special text manipulation commands in Vim
Category | Command | Action |
---|---|---|
On a Single Line | --- | |
~ | dd or D | Delete current line |
~ | cc or C | Change current line |
~ | yy | Yank (copy) current line |
On a Single Character | --- | |
~ |
| Delete current character |
~ |
| Replace current character with <char> |
~ |
| Change case of current character |
~ |
| Find matching parenthesis, bracket, or brace |
Table 5.8. Searching and substituting in Vim
Category | Command | Action |
---|---|---|
Searching Text | --- | |
~ |
| Search forward |
~ |
| Repeat search forwards |
~ |
| Search backward |
~ |
| Repeat search backwards |
Substituting Text | --- | |
~ |
| Substitute new for old for the first occurrence of old on the current line |
~ |
| Substitute new for old throughout the current line |
~ |
| Substitute new for old between lines numbered <line1> and <line2> |
~ |
| Substitute new for old throughout the entire buffer |
~ |
| Substitute new for old throughout the entire buffer, with a prompt for each substitution |
Table 5.9. Ways to enter insert mode in Vim
Command | Method of Entering Insert Mode |
---|---|
i | Insert text just before cursor |
a | Insert text just after cursor |
I | Insert text at start of current line |
A | Append text to end of current line |
O | Open a line just above current line |
o | Open a line just below current line |
Table 5.10. Other modes in Vim
Command | Mode |
---|---|
R | Replace mode |
v | Visual mode |
V | Visual line mode |
C-v | Visual block mode |
[37] Arnold Robbins, Elbert Hannah, and Linda Lamb, Learning the vi and Vim Editors, 7th ed (Sebastopol: O'Reilly, 2008), Chapter 1.
[38] Matthias Kalle Dalheimer and Matt Welsh, Running Linux, 5th ed., Section 19.1: “Editing Files Using vi.”
[39] An open-source version of the original vi is now available on SourceForge. Details can be found in Section A.4.1, “More on vi and Vim”.