Text editors are application programs which allow users to create new text files and/or modify the contents of existing text files. Many of us have used simple text editors, perhaps a popular one such as Microsoft's Notepad text editor.
Unix has many text editing applications available for use. Two of the more popular of these are vi (whose name comes from VIsual editor) and emacs (whose name is derived from Editor MACroS). The vi editor is typically a non-GUI based tool, whereas emacs is available in both GUI and non-GUI modes (however, most users prefer the GUI mode of emacs). Both of these text editing applications are included in most, if not all Unix installations. Since this e-text is focused upon command line (i.e. text) based applications, the discussion of text based editor programs will focus upon the vi editor. If additional information regarding the emacs editor is desired, it can be found at GNUs site here.
The vi editor is a text based editor with numerous, powerful features. This power sometimes overwhelms less experienced users and results in users shying away from vi. The key to using vi is to understand what it is you wish to do and how to accomplish doing it. While some of the commands may seem awkward at first, the key to becoming proficient with this or any other application is practice.
Commands to accomplish several necessary and useful tasks are listed below. Note these commands are a small subset of all vi commands, although these are most of the commands the author regularly uses.
Opening a File
To open a new or existing file using vi, you simply enter the command:
$ vi file_spec [Enter]where file_spec can be any existing text file specfication (for which you have read access) or the specification of a new file. Once in the vi editor, you will see a tilde character (~) on each line below the existing text. Some vi editor versions will also display the name of the file, the number of lines, characters, etc. for added user friendliness. Note you can also simply type vi [Enter] without a file_spec, which will result in your editing of an unnamed document.
:wq (or ZZ)This command will write your changes and quit the editing session. If you do not wish to save any changes (since your last write), you enter:
:qif you have made no changes since the last write or
:q!if you have made changes since your last write. Note that if you try to quit and get a message like
No write since last change (use ! to override)this indicates that you have made changes since last write and must quit with the :q! command.
i - inserts text to left of cursor a - append, inserts to right of cursor o - open a new line, inserts on the line below the cursor (at the beginning of the line)Recall, if you wish to leave insert mode and return to command mode, this is accomplished by pressing the [Esc] key.
h - move cursor left j - move cursor down k - move cursor up l - (ell) move cursor right (or you can use the spacebar) 0 - (zero) move cursor to beginning of line $ - move cursor to end of line nG - move to line number n (e.g. 3G moves to line 3) G - move to bottom of documentNote that the arrow keys may work on some terminals, but this is not guaranteed and it is suggested that the h, j, k, and l keys be used. Some question the use of the h, j, k, and l keys for movement. This is not as un-natural as it may seem since these keys are near the normal typing position of your right hand (i.e. r-hand home row position). With practice, you will find this very natural and movement very easy.
x - delete a single character dw - delete a single word dd - delete a single line nx, ndw, ndd deletes n chars, words or lines respectivelyKeep in mind that if text is deleted by mistake, the delete can be undone and redone correctly (see below).
ndd - cut (delete) n lines and place in buffer nyy - copy (yank) n lines and place in buffer p - paste buffer where cursor is positioned
To search for a text string within a file, you use the command /string followed by [Enter], where string is the string you wish to search for. You can also find subsequent occurrences of string with the command /[Enter].
To repeat a command in vi, simply use the . (dot) command. For instance, if you wish to delete 5 lines, you enter 5dd. If you then wish to delete 5 more, you can simply use the . command.
To undo the action of a previous command, simply use the u command. For instance, if you have deleted 5 lines by mistake, simply press the u key and the delete will be undone. In some vi implementations, you can even undo multiple actions.
vi In Summary
|Opening a file||$ vi file_spec [Enter]  (for new or existing files)|
|Exiting a file with save||:wq (or ZZ)|
|Exiting a file without save||:q (if no changes were made)|
|:q! (if the file was changed)|
|Two basic modes||command mode (entered with Esc key)|
|Entering insert mode||i (to insert to left of cursor)|
|a (append, insert to right of cursor)|
|o (open a new line)|
|Cursor movement||h - move cursor left|
|j - move cursor down|
|k - move cursor up|
|l - (ell) move cursor right (or use spacebar)|
|0 - (zero) move cursor to beginning of line|
|$ - move cursor to end of line|
|nG - move to line number n|
|G - move to bottom of document|
|Deleting text||x - delete a single character|
|dw - delete a single word|
|dd - delete a single line|
|nx, ndw, ndd deletes n chars, words or lines respectively|
|Cut/copy & paste||ndd - cut n lines and place in buffer|
|nyy - copy n lines and place in buffer|
|p - paste buffer where cursor is positioned|
|Miscellaneous||/string [Enter] - searches and moves to first occurrence of string|
|/[Enter] - searches and moves to next occurrence of string|
|. - redo last command|
|u - undo last command|
|r - replace (overwrite) a single character|
©2015, Mark A. Thomas. All Rights Reserved.