Go to the first, previous, next, last section, table of contents.

# A Survey of VIP

In this chapter we describe basics of VIP with emphasis on the features not found in Vi and on how to use VIP under GNU Emacs.

- Basic Concepts: Basic concepts in Emacs.
- Loading VIP: How to load VIP automatically.
- Modes in VIP: VIP has three modes, which are orthogonal to modes in Emacs.
- Differences from Vi: Differences of VIP from Vi is explained.

## Basic Concepts

We begin by explaining some basic concepts of Emacs. These concepts are explained in more detail in the GNU Emacs Manual.

Conceptually, a buffer is just a string of ASCII characters and two special characters `PNT` (point) and `MRK` (mark) such that the character `PNT` occurs exactly once and `MRK` occurs at most once. The text of a buffer is obtained by deleting the occurrences of `PNT` and `MRK`. If, in a buffer, there is a character following `PNT` then we say that point is looking at the character; otherwise we say that point is at the end of buffer. `PNT` and `MRK` are used to indicate positions in a buffer and they are not part of the text of the buffer. If a buffer contains a `MRK` then the text between `MRK` and `PNT` is called the region of the buffer.

Emacs provides (multiple) windows on the screen, and you can see the content of a buffer through the window associated with the buffer. The cursor of the screen is always positioned on the character after `PNT`.

A keymap is a table that records the bindings between characters and command functions. There is the global keymap common to all the buffers. Each buffer has its local keymap that determines the mode of the buffer. Local keymap overrides global keymap, so that if a function is bound to some key in the local keymap then that function will be executed when you type the key. If no function is bound to a key in the local map, however, the function bound to the key in the global map becomes in effect.

## Loading VIP

The recommended way to load VIP automatically is to include the line:

    (load "vip")

in your ``.emacs'` file. The ``.emacs'` file is placed in your home directory and it will be executed every time you invoke Emacs. If you wish to be in vi mode whenever Emacs starts up, you can include the following line in your ``.emacs'` file instead of the above line:

    (setq term-setup-hook 'vip-mode)

(See section Vi Mode, for the explanation of vi mode.)

Even if your ``.emacs'` file does not contain any of the above lines, you can load VIP and enter vi mode by typing the following from within Emacs.

    M-x vip-mode

## Modes in VIP

Loading VIP has the effect of globally binding `C-z` (`Control-z`) to the function `vip-change-mode-to-vi`. The default binding of `C-z` in GNU Emacs is `suspend-emacs`, but, you can also call `suspend-emacs` by typing `C-x C-z`. Other than this, all the key bindings of Emacs remain the same after loading VIP.

Now, if you hit `C-z`, the function `vip-change-mode-to-vi` will be called and you will be in vi mode. (Some major modes may locally bind `C-z` to some special functions. In such cases, you can call `vip-change-mode-to-vi` by `execute-extended-command` which is invoked by `M-x`. Here `M-x` means `Meta-x`, and if your terminal does not have a `META` key you can enter it by typing ``ESC` x`. The same effect can also be achieve by typing `M-x vip-mode`.)

You can observe the change of mode by looking at the mode line. For instance, if the mode line is:

    -----Emacs: *scratch*              (Lisp Interaction)----All------------

then it will change to:

    -----Vi:    *scratch*              (Lisp Interaction)----All------------

Thus the word ``Emacs'` in the mode line will change to ``Vi'`.

You can go back to the original emacs mode by typing `C-z` in vi mode. Thus `C-z` toggles between these two modes.

Note that modes in VIP exist orthogonally to modes in Emacs. This means that you can be in vi mode and at the same time, say, shell mode.

Vi mode corresponds to Vi's command mode. From vi mode you can enter insert mode (which corresponds to Vi's insert mode) by usual Vi command keys like `i`, `a`, `o` ... etc.

In insert mode, the mode line will look like this:

    -----Insert *scratch*              (Lisp Interaction)----All------------

You can exit from insert mode by hitting `ESC` key as you do in Vi.

That VIP has three modes may seem very complicated, but in fact it is not so. VIP is implemented so that you can do most editing remaining only in the two modes for Vi (that is vi mode and insert mode).

- Emacs Mode: This is the mode you should know better.
- Vi Mode: Vi commands are executed in this mode.
- Insert Mode: You can enter text, and also can do editing if you know enough Emacs commands.

### Emacs Mode

You will be in this mode just after you loaded VIP. You can do all normal Emacs editing in this mode. Note that the key `C-z` is globally bound to `vip-change-mode-to-vi`. So, if you type `C-z` in this mode then you will be in vi mode.

### Vi Mode

This mode corresponds to Vi's command mode. Most Vi commands work as they do in Vi. You can go back to emacs mode by typing `C-z`. You can enter insert mode, just as in Vi, by typing `i`, `a` etc.

### Insert Mode

The key bindings in this mode is the same as in the emacs mode except for the following 4 keys. So, you can move around in the buffer and change its content while you are in insert mode.

``ESC``
  This key will take you back to vi mode.
`C-h`
  Delete previous character.
`C-w`
  Delete previous word.
`C-z`
  Typing this key has the same effect as typing `ESC` in emacs mode. Thus typing `C-z x` in insert mode will have the same effect as typing `ESC x` in emacs mode.

## Differences from Vi

The major differences from Vi are explained below.

- Undoing: You can undo more in VIP.
- Changing: Commands for changing the text.
- Searching: Search commands.
- z Command: You can now use zH, zM and zL as well as z- etc.
- Counts: Some Vi commands which do not accept a count now accept one.
- Marking: You can now mark the current point, beginning of the buffer etc.
- Region Commands: You can now give a region as an argument for delete commands etc.
- New Commands: Some new commands not available in Vi are added.
- New Bindings: Bindings of some keys are changed for the convenience of editing under Emacs.
- Window Commands: Commands for moving among windows etc.
- Buffer Commands: Commands for selecting buffers etc.
- File Commands: Commands for visiting files etc.
- Misc Commands: Other useful commands.

### Undoing

You can repeat undoing by the `.` key. So, `u` will undo a single change, while `u . . .`, for instance, will undo 4 previous changes. Undo is undoable as in Vi. So the content of the buffer will be the same before and after `u u`.

### Changing

Some commands which change a small number of characters are executed slightly differently. Thus, if point is at the beginning of a word ``foo'` and you wished to change it to ``bar'` by typing `c w`, then VIP will prompt you for a new word in the minibuffer by the prompt ``foo => '`. You can then enter ``bar'` followed by `RET` or `ESC` to complete the command. Before you enter `RET` or `ESC` you can abort the command by typing `C-g`. In general, you can abort a partially formed command by typing `C-g`.

### Searching

As in Vi, searching is done by `/` and `?`. The string will be searched literally by default. To invoke a regular expression search, first execute the search command `/` (or `?`) with empty search string. (I.e, type `/` followed by `RET`.) A search for empty string will toggle the search mode between vanilla search and regular expression search. You cannot give an offset to the search string. (It is a limitation.) By default, search will wrap around the buffer as in Vi. You can change this by rebinding the variable `vip-search-wrap-around`. See section Customization, for how to do this.

### z Command

For those of you who cannot remember which of `z` followed by `RET`, `.` and `-` do what. You can also use `z` followed by `H`, `M` and `L` to place the current line in the Home (Middle, and Last) line of the window.

### Counts

Some Vi commands which do not accept a count now accept one

`p`
`P`
  Given counts, text will be yanked (in Vi's sense) that many times. Thus `3 p` is the same as `p p p`.
`o`
`O`
  Given counts, that many copies of text will be inserted. Thus `o a b c `ESC`` will insert 3 lines of ``abc'` below the current line.
`/`
`?`
  Given a count n, n-th occurrence will be searched.

### Marking

Typing an `m` followed by a lower case character ch marks the point to the register named ch as in Vi. In addition to these, we have following key bindings for marking.

`m <`
  Set mark at the beginning of buffer.
`m >`
  Set mark at the end of buffer.
`m .`
  Set mark at point (and push old mark on mark ring).
`m ,`
  Jump to mark (and pop mark off the mark ring).

### Region Commands

Vi operators like `d`, `c` etc. are usually used in combination with motion commands. It is now possible to use current region as the argument to these operators. (A region is a part of buffer delimited by point and mark.) The key `r` is used for this purpose. Thus `d r` will delete the current region. If `R` is used instead of `r` the region will first be enlarged so that it will become the smallest region containing the original region and consisting of whole lines. Thus `m . d R` will have the same effect as `d d`.

### Some New Commands

Note that the keys below (except for `R`) are not used in Vi.

`C-a`
  Move point to the beginning of line.
`C-n`
  If you have two or more windows in the screen, this key will move point to the next window.
`C-o`
  Insert a newline and leave point before it, and then enter insert mode.
`C-r`
  Backward incremental search.
`C-s`
  Forward incremental search.
`C-c`
`C-x`
``ESC``
  These keys will exit from vi mode and return to emacs mode temporarily. If you hit one of these keys, Emacs will be in emacs mode and will believe that you hit that key in emacs mode. For example, if you hit `C-x` followed by `2`, then the current window will be split into 2 and you will be in vi mode again.
`\`
  Escape to emacs mode. Hitting `\` will take you to emacs mode, and you can execute a single Emacs command. After executing the Emacs command you will be in vi mode again. You can give a count before typing `\`. Thus `5 \ *`, as well as `\ C-u 5 *`, will insert ``*****'` before point. Similarly `1 0 \ C-p` will move the point 10 lines above the current line.
`K`
  Kill current buffer if it is not modified. Useful when you selected a buffer which you did not want.
`Q`
`R`
  `Q` is for query replace and `R` is for replace. By default, string to be replaced are treated literally. If you wish to do a regular expression replace, first do replace with empty string as the string to be replaced. In this way, you can toggle between vanilla and regular expression replacement.
`v`
`V`
  These keys are used to Visit files. `v` will switch to a buffer visiting file whose name can be entered in the minibuffer. `V` is similar, but will use window different from the current window.
`#`
  If followed by a certain character ch, it becomes an operator whose argument is the region determined by the motion command that follows. Currently, ch can be one of `c`, `C`, `g`, `q` and `s`.
`# c`
  Change upper case characters in the region to lower case (`downcase-region`).
`# C`
  Change lower case characters in the region to upper case. For instance, `# C 3 w` will capitalize 3 words from the current point (`upcase-region`).
`# g`
  Execute last keyboard macro for each line in the region (`vip-global-execute`).
`# q`
  Insert specified string at the beginning of each line in the region (`vip-quote-region`).
`# s`
  Check spelling of words in the region (`spell-region`).
`*`
  Call last keyboard macro.

### New Key Bindings

In VIP the meanings of some keys are entirely different from Vi. These key bindings are done deliberately in the hope that editing under Emacs will become easier. It is however possible to rebind these keys to functions which behave similarly as in Vi. See section Customizing Key Bindings, for details.

`C-g`
`g`
  In Vi, `C-g` is used to get information about the file associated to the current buffer. Here, `g` will do that, and `C-g` is used to abort a command (this is for compatibility with emacs mode.)
`SPC`
``RET``
  Now these keys will scroll up and down the text of current window. Convenient for viewing the text.
`s`
`S`
  They are used to switch to a specified buffer. Useful for switching to already existing buffer since buffer name completion is provided. Also a default buffer will be given as part of the prompt, to which you can switch by just typing `RET` key. `s` is used to select buffer in the current window, while `S` selects buffer in another window.
`C`
`X`
  These keys will exit from vi mode and return to emacs mode temporarily. If you type `C` (`X`), Emacs will be in emacs mode and will believe that you have typed `C-c` (`C-x`, resp.) in emacs mode. Moreover, if the following character you type is an upper case letter, then Emacs will believe that you have typed the corresponding control character. You will be in vi mode again after the command is executed. For example, typing `X S` in vi mode is the same as typing `C-x C-s` in emacs mode. You get the same effect by typing `C-x C-s` in vi mode, but the idea here is that you can execute useful Emacs commands without typing control characters. For example, if you hit `X` (or `C-x`) followed by `2`, then the current window will be split into 2 and you will be in vi mode again.

In addition to these, `ctl-x-map` is slightly modified:

`X 3`
`C-x 3`
  This is equivalent to `C-x 1 C-x 2` (1 + 2 = 3).

### Window Commands

In this and following subsections, we give a summary of key bindings for basic functions related to windows, buffers and files.

`C-n`
  Switch to next window.
`X 1`
`C-x 1`
  Delete other windows.
`X 2`
`C-x 2`
  Split current window into two windows.
`X 3`
`C-x 3`
  Show current buffer in two windows.

### Buffer Commands

`s`
  Switch to the specified buffer in the current window (`vip-switch-to-buffer`).
`S`
  Switch to the specified buffer in another window (`vip-switch-to-buffer-other-window`).
`K`
  Kill the current buffer if it is not modified.
`X S`
`C-x C-s`
  Save the current buffer in the file associated to the buffer.

### File Commands

`v`
  Visit specified file in the current window.
`V`
  Visit specified file in another window.
`X W`
`C-x C-w`
  Write current buffer into the specified file.
`X I`
`C-x C-i`
  Insert specified file at point.

### Miscellaneous Commands

`X (`
`C-x (`
  Start remembering keyboard macro.
`X )`
`C-x )`
  Finish remembering keyboard macro.
`*`
  Call last remembered keyboard macro.
`X Z`
`C-x C-z`
  Suspend Emacs.
`Z Z`
  Exit Emacs.
`Q`
  Query replace.
`R`
  Replace.

Go to the first, previous, next, last section, table of contents.