Introduction to Text Editors: Vi(m)

Before we dive deep into the topic, you must have wondered what "vi(m)" means. I wrote it that way because any command I would be teaching you about applies to both vi and vim text editors. VIM stands for Vi IMproved, so you get the gist.

The default editor bundled with the UNIX operating system is referred to as vi, short for "visual editor". Other alternative editors for UNIX environments include pico and emacs, which is a product of GNU. The vi editor in UNIX operates in two modes:

  1. Command mode, where commands are entered to perform actions on the file.

  2. Insert mode, where typed text is inserted into the file.

In command mode, each character entered is a command affecting the text file, and typing a character might switch vi to insert mode. In insert mode, every typed character is added to the file, and pressing the <Esc> (Escape) key turns off insert mode.

Though there are numerous vi commands, beginners often find just a few essential commands sufficient. This webpage offers a selection of basic vi commands, with asterisks (*) marking the most fundamental and useful ones. With practice, these commands should become second nature.

It's important to note that both UNIX and vi are case-sensitive. Using a capital letter instead of a lowercase one can yield unexpected results.

Basic Vi(m) Commands

Outlined in the table below are the basic vi/vim commands. Remember that "*" is not part of the commands, but to tell you that those commands marked with asterisks are the fundamental and most useful commands.

COMMANDRESULT OF COMMAND
* vi <filename>edit filename, starting from the first line.
vi -r <filename>recover filename that was being edited when an error occurred.
* :x, then <Return/Enter>quits vi(m) and saves the file with the name specified before the edit is made.
:wq, then <Return/Enter>quits vi(m) and saves the file with the name specified before the edit is made.
:q, then <Return/Enter>quits vi(m) (gives an error if the latest changes of the file have not been saved).
* :q!, then <Return/Enter>quits vi(m) even though the latest changes of the file have not been saved.
*j or <Return/Enter> or [down-arrow]moves the cursor down one line.
*k or [up-arrow]moves the cursor up one line.
*h or <Backspace> or [left-arrow]moves the cursor left one character.
*l(small letter L) or <Space> or [right-arrow]moves the cursor right one character.
*0 (zero)moves the cursor to the start of the current line.
*$moves the cursor to the end of the current line.
wmoves the cursor to the beginning of the next word.
bmoves the cursor to the beginning of the preceding word.
:0(zero), then <Return/Enter> or 1Gmoves the cursor to the first line in the file.
:n, then <Return/Enter> or nGmoves the cursor to line n in the file.
:$, then <Return/Enter> or Gmoves the cursor to the last line in the file.
^fmoves forward one screen(page).
^bmoves backward one screen(page).
^dmoves down (forward) one-half screen.
^umove up (backward) one-half screen.
^l (small letter L)redraws the screen.
^rredraws the screen, removing deleted lines.
*uundo whatever you just did.
*iinserts text before the cursor, until <Esc> is hit.
I (capital letter i)inserts text at the beginning of the current line, until <Esc> is hit.
*aappends text after the cursor, until <Esc> is hit.
Aappends text to the end of the current line, until <Esc> is hit.
*o(small letter)opens and puts text in a new line below the current line, until <Esc> is hit.
O (capital letter)opens and puts text in a new line above the current line, until <Esc> is hit.
*rreplaces the single character under the cursor (no <Esc> needed).
Rreplaces characters, starting with the current cursor position, until <Esc> is hit.
cwchanges the current word with new text, starting with the character under the cursor, until <Esc> is hit.
cNwchanges N words beginning with character under the cursor, until <Esc> is hit. For example, c3w changes 3 words.
Cchanges (replaces) the characters in the current line, until <Esc> is hit.
ccchanges (replaces) the entire current line, stopping when <Esc> is hit.
Ncc or cNcchanges (replaces) the next N lines, starting with the current line, stopping when <Esc> is hit.
*xdeletes the single character under the cursor.
Nxdeletes N characters, starting with the character under the cursor.
dwdeletes the single word beginning with the character under the cursor.
dNwdeletes N words, beginning with the character under the cursor. For example, d3w deletes 3 words.
Ddeletes the remainder of the line, starting with the current cursor position.
*dddeletes the entire current line.
Ndd or dNddeletes N lines, beginning with the current line. For example, 3dd deletes 3 lines.
yycopies (yanks, cuts) the current line into the buffer.
Nyy or yNycopies (yanks, cuts) the next N lines, including the current line, into the buffer.
ppastes the line(s) in the buffer into the text space after the current line.
/stringsearches forward for the occurrence of the string in the text.
?stringsearches backwards for the occurrence of the string in the text.
nmoves to the next occurrence of the search string.
Nmoves to the next occurrence of the search string in the opposite direction.
:.=returns the line number of the current line at the bottom of the screen.
:=returns the total number of lines at the bottom of the screen.
^gprovides the current line number, along with the total number of lines, in the file at the bottom of the screen.
:r <filename> <return>reads the file named filename and inserts after the current line.
:w <return>writes the current contents to the file named in the original vi(m) call.
:w <newfile> <return>writes current contents to a new file named newfile.
:12, 35w <smallfile> <return>writes the contents of the lines numbered 12 through 35 to a new file named smallfile.
:w! <prevfile> <return>writes current contents over a pre-existing file named prevfile.

Note Below:

  1. Unlike PC editors, you cannot highlight and delete text here, you can only use commands.

  2. The undo command acts like a toggle, undoing and redoing your last action. You cannot go back more than one step.

  3. Unlike PC editors, the mouse cannot move the cursor here. You must use the key commands outlined above. Arrow keys may also work for some UNIX shells but are better avoided because they may produce unwanted behaviour.

  4. The cursor moves to the bottom of the screen whenever the ":" (semi-colon) is typed (when not in insert mode).

  5. When you type "vi(m) filename", the editor will open for you to start editing from the first line only when "filename" didn't previously exist. If it does, the editor will open to the first page of the already existing file.

Are you lost? The link to the first topic of this series is embedded below.

Conclusion

I trust that your enjoyment in reading this matches the pleasure I experienced while composing it. If you would like to see more content of the same nature, then navigate to the menu and follow my socials. Here, I record the majority of what I'm learning and the projects I'm currently engaged in.