Learning the vi Editor/Vim/Modes
Modes[edit | edit source]
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 ESC to get back to normal mode.)
Here's a short overview of each mode available in vim:
|normal||For navigation and manipulation of text. This is the mode that vim will usually start in, which you can usually get back to with ESC.|
|insert||For inserting new text. The main difference from vi is that many important "normal" commands are also available in insert mode - provided you have a keyboard with enough meta keys (such as Ctrl, Alt, Windows-key, etc.).|
|visual||For navigation and manipulation of text selections, this mode allows you to perform most normal commands, and a few extra commands, on selected text.|
|select||Similar to visual, but with a more MS Windows-like behavior.|
|command-line||For entering editor commands - like the help commands in the 3rd column.|
|Ex-mode||Similar to the command-line mode but optimized for batch processing.|
Each mode is described below.
insert (and replace)[edit | edit source]
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 <a> (append after cursor), <i> (insert before cursor), <A> (append at end of line), <I> (insert at beginning of line), <C> (change to end of line), and <s> (substitute characters).
If you wish to edit text by selecting and replacing, as is common in many GUI-based editors, <C> does nicely. The selected text is deleted before entering insert mode.
normal (command)[edit | edit source]
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 <Ctrl-O> followed by the desired command. After one command, the user is returned to insert mode. (Each normal command must be started first by pressing <Ctrl-O>).You can also enter to command mode from insert mode by pressing Esc key.
visual[edit | edit source]
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 (<Ctrl-~>), deleting text (<d>), 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[edit | edit source]
The plain visual mode is started by pressing 'v' in normal mode. At any point, pressing ESC or <v> 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 <u>).
block visual mode[edit | edit source]
block-visual is started by pressing <Ctrl-V> (or <Ctrl-Q> 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 <Ctrl-V>. 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 <ESC>, 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 '<I>' or '<A>' rather than '<i>' or '<a>'.)
linewise visual mode[edit | edit source]
In linewise-visual mode, enterd by <Shift-V>, entire lines are highlighted. Otherwise, it generally works like the plain visual mode.
select[edit | edit source]
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 <U> 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[edit | edit source]
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:
:set number :substitute/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,
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
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[edit | edit source]
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
Q in normal mode and leave it again with the
:visual 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 :%substitute/CFLAGS = -g$/CFLAGS =-fPIC -DPIC -g/ :%substitute/CFLAGS =$/CFLAGS =-fPIC -DPIC/ :%substitute/ADAFLAGS =$/ADAFLAGS =-fPIC -DPIC/ :update :quit 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.
vim -E -s
- starts vim in improved Ex mode which allows for more advanced commands than the vi compatible Ex-mode (which is started with
vim -e -s).
- 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
-c option in combination with the
vim -E -s -c "source Makefile-Fix1.vim" Makefile
- 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.