Learning the vi Editor/Vim/Modes

Modes
VIM offers more modes than vi (which offers only the "normal", "insert" and "command–line" modes). These additional modes make VIM more powerful and easier to use; because of this, vim users should at least be aware that they exist. (NOTE: If you ever enter a mode you are unfamiliar with, you can usually press to get back to normal mode.)

Here's a short overview of each mode available in vim:

Each mode is described below.

insert (and replace)
In insert mode you can type new text. In classic vi the insert mode was just that: insert text and nothing else. Vim makes use of many meta keys on modern keyboards; with a correctly configured vim, cursor keys should work in insert mode.

Insert mode can be reached in several ways, but some of the most common ones are (append after cursor),  (insert before cursor) ,  (append at end of line) ,  (insert at beginning of line) ,  (change to end of line) , and  (substitute characters).

If you wish to edit text by selecting and replacing, as is common in many GUI-based editors, does nicely. The selected text is deleted before entering insert mode.

normal (command)
Unless you use the evim interface this is the standard mode for vim (vim starts in normal mode). Everything the user types in normal mode is interpreted as commands (including those which switch the user to other modes).

If vim is started as evim (evim on the command line), vim keeps the user in insert mode all the time. Normal mode can be reached for individual commands by pressing followed by the desired command. After one command, the user is returned to insert mode. (Each normal command must be started first by pressing ).You can also enter to command mode from insert mode by pressing Esc key.

visual
There are three different types of highlighting in visual mode. Each allows the user to highlight text in different ways. Commands that normally only affect one character, line, or area will affect the highlighted text (such as changing text to uppercase, deleting text , indenting lines (>>, <<, and =), and so forth).

There are three (sub)types of the visual modes which are visual, block-visual, and linewise-visual

plain visual mode
The plain visual mode is started by pressing 'v' in normal mode. At any point, pressing ESC or will leave VISUAL mode without performing an operation. Movement commands change the selection area, while other commands will generally perform the expected operation on the text (there are some exceptions where the behavior will change or where the command won't work, but if it doesn't do what you hoped you can always undo with ).

block visual mode
block-visual is started by pressing (or  in some windows versions.  If neither of these works use ":help visual-block" to find out how). Visual blocks always maintain a rectangular selection, highlighting only specific columns of characters over multiple lines. In this following example the user wants to put a dash in each phone number between the second and third number fields:

The user first moves the cursor to the top of the column (you could start at the bottom if you want).

Next, press. This puts you in block-visual mode (VISUAL BLOCK appears at the bottom to tell you what visual mode you're in). Next, move down to the bottom desired line. You can see a single column highlighted in this example, but you could move right or left and highlight more columns.

In this case, the user wants to change the spaces to dashes. To change text, we press 'c'. The spaces all disappear, and the changes are shown only in the current line while we type:

when we press, though, the change is duplicated on all the lines.

(Note: if you simply want to insert text rather than change it, you will need to use  or  rather than  or .)

linewise visual mode
In linewise-visual mode, enterd by, entire lines are highlighted. Otherwise, it generally works like the plain visual mode.

select
like the visual mode but with more CUA like behavior. This means that if you type a single character it replaces the selection. Of course you lose all the one key operation on selection like to make a selection uppercase.

This mode is usually activated by:



which is default for MS-Windows installations. You can get the normal mode with



command-line
Within the command-line you can run Ex commands, enter search patterns, and enter filter commands. At the bottom a command line appears where you can enter the command. Unlike vi - vim supports cursor keys which makes entering commands a lot easier. After one command the editor returns into normal mode.

You can enter an Ex command by typing a colon  in normal mode. Some examples include:

:/search/replace/ig

You can enter a search pattern by typing a slash  to search forward, or a question mark   to search backward. You can use vim's expanded regular expressions in these search patterns. For example,

/word

will jump to the next occurrence of "word" (even if it is "sword" or "wordlessly"), but

/\

will jump only to a complete word "word" (not "sword" or "wordless").

You can enter a filter by typing followed by a motion command, then a shell command to run on the text captured by the motion. For example, in Linux and other UNIX-like operating systems, typing

22jsort

will sort the current and 22 following lines with the sort system command. The same thing can be done with



As a matter of fact, vim creates the above command for you if you follow the first example!

Ex-mode
The Ex mode is similar to the command line mode as it also allows you to enter Ex commands. Unlike the command-line mode you won't return to normal mode automatically. You can enter an Ex command by typing a  in normal mode and leave it again with the   command. Note that the Ex mode is designed for Batch processing and as such won't support mappings or command-line editing.

For batch processing the Ex-mode is normally started from outside by calling the editor with the "-E" option. Here is a real live example from the RPM Package Manager specification:

vim -E -s Makefile <<-EOF :%/CFLAGS = -g$/CFLAGS =-fPIC -DPIC -g/ :%/CFLAGS =$/CFLAGS =-fPIC -DPIC/ :%/ADAFLAGS =$/ADAFLAGS =-fPIC -DPIC/ :   : EOF

The RPM uses Bash as script language which make the example a little difficult to understand as two different script languages are mixed in one file.


 * : starts vim in improved Ex mode which allows for more advanced commands than the vi compatible Ex-mode (which is started with ).
 * : tells bash to copy all lines that follow into the standard input of the external program just started. The '-' tells the shell to strip the tab characters.
 * : are lines with Ex commands which vim will execute. The  is optional but helpful when two script languages are mixed in one file
 * : A beginners mistake is to forget to actually save the file after the change - falsely assuming that this happens automatically.
 * : Last not least: don't forget to actually exit vim again.
 * : marks the end of the standard input redirection - from now on bash will execute the command itself again.

If your shell does not allow such nifty redirection of standard input then you can always use a more classic approach to I/O redirection using two files:

vim -E -s Makefile <Makefile-Fix1.vim

And if you have no standard input redirection available then you can try the  option in combination with the   command:

vim -E -s -c " Makefile-Fix1.vim" Makefile

With the improved Ex mode many tasks classically performed by awk or sed can be done with vim and often better so:


 * awk and sed are stream oriented - they only read the file forward from beginning to end while vim is buffer oriented - you can move forward and backward in the file as you like.
 * vim's regular expressions are more powerful than awk's and sed's expressions - for example vim can match over several lines and supports zero matches.