Vi is a text editor for the console. It is used in Unix-like operating systems.

    file

    There are several versions of vi: vi, elvis, vile, and vim. One of them is available in almost all versions of Unix as well as Linux. It has a large number of features, including syntax highlighting, code formatting, a powerful substitution search engine, macros, and more.

    Unlike many familiar editors, vi has a modal interface. This means that the same keys perform different actions in different modes

    The vi editor has two basic modes

    • command mode (enabled by default).
    • insert mode.

    In insert mode the keyboard is used to type. To exit to command mode use key Esc or combination Ctrl+c.

    In command mode alphabetic keys correspond to commands of text moving and changing. Commands h, j,k, l move the cursor one position left, down, up, right respectively, command x removes one character, etc

    This allows you to work without the need to use additional keyboard and modifier keys like Ctrl, Alt, etc. More complex editing operations are obtained by combining simple ones, e.g. 2dw deletes two words.

    Frequently used commands:

    • /str - Find string str forward. str can be a regular expression
    • ?str - Search str string backward
    • n - Repeat search in the same direction
    • N - Repeat search in reverse direction
    • :[range]s/old/new/[g] - Replace old with new in the specified range of strings. new and old can be regular expressions, and range is set similarly to the range of strings in the ed editor. For example, for a range of strings 32 to 64 range would be 32,64, the first string corresponds to 1, the last is set with a $ character. The suffix g means to replace all occurrences of old in the string, not just the first.
    • :e! - reload the current file
    • :33 - move to the 33rd line of the text file
    • i - enter edit mode
    • a - enter edit mode after the current character
    • u - cancel last action
    • . - repeat last action
    • x - delete character under the cursor
    • yy - copy line
    • dd - cut string
    • p - paste
    • J - glue two lines
    • :w - save file to disk
    • :wq - exit with file saved (shift + zz)
    • :q - exit
    • :q! - exit without saving file
    • :r - insert another file into the document

    For detailed help on the vi editor, run the command man vi in the Unix shell (q - exit from help).

    More information can be found here.