VI and Vim Tutorial
Linux vi and vim text editor tutorial
What is vi?
Vi is one of the most popular text editors available under Unix and Linux. Vi was originally created for Unix, and has over the years become one of the most popular text editors. Vi was originally created back in 1976 as a visual mode for the "ex" editor.
Vi generally operates in two modes: "insert" mode, where text becomes part of the document and "normal" mode, where keystrokes are interpreted as commands that control the editing functions. For example, pressing the "Escape" key and typing an "i" while in normal mode switches the editor to insert mode, but typing i again at this point places an "i" character in the document. From insert mode, pressing the "Escape" key switches the editor back to normal mode.
What is vim?
Vim stands for "Vi Improved" It is a text editor that is based on vi, however, it has more features. These features comprise of mouse support, syntax highlighting, plus numerous other commands. Today, Vim is shipped with almost all Linux distributions.
VI (Vim) Basic Commands
|Command Mode||[Esc]||Exit editing mode. Keyboard keys now interpreted as commands.|
|Moving the Cursor||h (or left arrow key)||Move the cursor left|
|l (or right arrow key)||Move the cursor right|
|j (or down arrow key)||Move the cursor down|
|k (or up arrow key)||Move the cursor up|
|^||Move cursor to the first none-white character in current line|
|$||Move the cursor to the end of the current line|
|G||Go to the last line in the file|
|nG||Go to line number n|
|Ctrl G||Display name of current file and the current cursor position|
|Entering Edit Mode||i||insert new text before the cursor|
|a||Append new text after the cursor|
|o||Start to edit a new line after the current one|
|O||Start to edit a new line before the current one|
|Replacing Characters||r||Replace the current character (Doesn't enter edit mode)|
|s||Enter edit mode and substitute the current character by several ones|
|cw||Change the word after the cursor|
|C||Enter edit mode and change the rest of the line after the cursor|
|Copying and Pasting||yy||copy (yank) the current line to the copy/paste buffer|
|p||Pastes the copy/paste buffer after the current line|
|P||Pastes the copy/paste buffer before the current line|
|Deleting Text||x||x Deletes the character at the cursor location|
|dw||Deletes the current word|
|D||Deletes the remainder of the line after the cursor|
|dd||Deletes the current line|
|Repeating Commands||.||Repeat the last insertion, replacement or delete command|
|String Search||/||/string (Find the first occurrence of string after the cursor)|
|?||?string (Find the first occurrence of string before the cursor)|
|n||Find the next occurrence in the last search|
|Replacing Strings||[Esc]:%s/string1/string2/g||Replaces string1 with string2 globally within the file|
|Apply command Multiple times||4j||Move the cursor 4 lines down|
|1G||Go to the 1st line in file|
|Miscellaneous Commands||Ctrl l||Redraw the screen|
|J||Join the current line with the next one|
|Saving and Exiting||ZZ||Save current file and exits editor|
|[Esc]:wq!||Save Changes and Exit|
|[Esc]:w||Saves Current File|
|[Esc]q!||Exit without saving|
|[Esc]w: filename||Save current work to filename|
Although initially using vi or vim can seem quite daunting. It is well worth the effort. Remember, if you are planning on working on a Linux server, the graphical GUI (desktop) may not be available and vi may be your only choice.
The above commands only scratch the surface of what can actually be achieved in vi and vim. To get further information on using vi or vim, you can go through a tutorial. To activate the tutorial, type vimtutor at your command line.