Mac Terminal vi[m] Editor Commands

A text editor is an application that enables you to view, create, or modify the contents of text files. Text editors were originally created to write programs in source code, but are now used to edit a wide variety of text-based files. Various types of text editors are compatible with Linux. However, text editors do not always support the formatting options that word processors provide. Text editors may work either in the CLI or GUI, and may have different modes of operation.

Vi is one of two powerhouse text editors in the Unix world, the other being EMACS. While obtuse, vi is extremely powerful and efficient. There may be times when vi is the only text editor available, so it helps to at least know the basics. On Mac OS X (and Linux), vi is symlinked to vim (vi improved), a more modern free software version. Vim It is the default editor when changing a crontab.

The vim command invokes the Vim editor. However, the vi command may also be used for this purpose because it automatically redirects the user to Vim. When entered without a file name as an argument, the vim command opens a welcome screen by default. Use the syntax vim {file name} to open a file. If the file does not exist, Vim creates a file by the name specified and opens the file for editing. Vim supports multiple files being opened simultaneously.

Note: a chunk of this small guide came from a web page I found long ago, but I don’t remember where so I can’t give proper credit. I’ve added and changed things from the original text.

vi[m] Modes

Vi is a modal editor, and its different modes decide the functionality of various keys.

Mode Enables Users To
Insert Insert text by typing.
Execute Execute commands within the editor.
Command Perform different editing actions using single keystrokes.
Visual Highlight or select text for copying, deleting, and so on.

Switch Modes

Command mode is the default mode of Vim, but you can switch from command mode to any other mode by using a single keystroke. Some of the keys to switch modes are listed here.

Key Function
i Switches to insert mode and inserts text to the left of the cursor.
A Switches to insert mode and adds text at the end of a line.
I Switches to insert mode and inserts text at the beginning of a line.
o Switches to insert mode and inserts text on a new line below the cursor.
O Switches to insert mode and inserts text on a new line above the cursor.
v Switches to visual mode to enable selection, one character at a time. V Switches to visual mode to enable selection, one line at a time.
: Switches to execute mode to enable users to enter commands.
Esc Returns to command mode.

Execute Mode Commands

In command mode, when you enter the colon (:) operator, a small command prompt section appears at the bottom-left of the editor. This indicates that you are in execute mode and can run commands supported by Vim. Some commands supported by Vim are listed in the following table.

Command Function
:w {file name} Saves a file with a file name if it is being saved for the first time.
:q Quits when no changes have been made after the last save.
:q! Quits, ignoring the changes made.
:qa Quits multiple files.
:wq Saves the current file and quits.
:e! Reverts to the last saved format without closing the file.
:!{any Linux command} Executes the command and displays the result in the Vim interface.
:help Opens Vim’s built-in help documentation.

Motions

Motions are single-key shortcuts that are used to navigate through files in command mode. These keys position the cursor anywhere within a document. They can be used for moving the cursor through characters, words, lines, or even huge blocks of text.

Navigation key Used To
h Move left one character.
j Move down one line.
k Move up one line.
l Move right one character.
^ Move to the beginning of the current line.
$ Move to the end of the current line.
w Move to the next word.
b Move to the previous word.
e Move to the end of the current word or to the end of the next word if you are already at the end of the word.
Shift+L Move the cursor to the bottom of the screen.
Shift+H Move the cursor to the first line of the screen.
(Line number) Shift+G Move the cursor to the specified line number.
gg Move the cursor to the first line of the file.
Shift+G Move the cursor to the last line of the file.

Navigation Using the Arrow Keys

In addition to using the h, j, k, and l keys to navigate through the editor, you can also use the Up, Down, Left, and Right Arrow keys. The conventional navigation keys such as Home, End, Page Up, and Page Down also work in Vim.

Editing Operators

Editing operators in command mode are powerful tools that can be used to manipulate text with simple keystrokes. They can also be used in combination with motions to edit multiple characters. Some of the frequently used editing operators are listed here.

Editing Operator Used To
x Delete the character selected by the cursor.
d Delete text.
dd Delete the current line.
p Paste text on the line directly below the cursor.
P Paste text on the line directly above the cursor.
/{text string} Search through the document for specific text.
?{text string} Search backward through the document for specific text.
y Copy text.
yy Copy the line directly above the cursor.
c{range of lines}c Begin a change in the specified range.
u Undo the latest change.
U Undo all changes in the current line.
ZZ Write the file only if changes were made, then quit the Vim editor.

Counts

A count is a number that multiplies the effect of keystrokes in Vim. It can be used in combination with motions, operators, or both. When used with a motion, cursor movement is multiplied according to the count specified. When used with editing operators, the action gets repeated the number of times specified.

The syntax for using a count with an operator and a motion is operator:

[count] {motion}
Related Post