What does CTRL+V do in vim

keyboard shortcutsvim

In vim while in insert mode, if I press CTRL+V I'm thrown into what is known as x mode (^[,^D...) and when I press a key (the escape key for instance) I end up with ^[ or something else that starts with ^. It also turns green in my editor.

What is this, and what is it used for?

Best Answer

From :h i_CTRL-V (the i_ indicates insert mode):

                                                i_CTRL-V
CTRL-V          Insert next non-digit literally.  For special keys, the
                terminal code is inserted.  It's also possible to enter the
                decimal, octal or hexadecimal value of a character
                i_CTRL-V_digit.
                The characters typed right after CTRL-V are not considered for
                mapping.  {Vi: no decimal byte entry}
                Note: When CTRL-V is mapped (e.g., to paste text) you can
                often use CTRL-Q instead i_CTRL-Q.

So, when you do ^v Esc, you're literally entering the Esc character into the text - Vim will not do whatever it usually does. The Esc character is usually represented as ^[, the ^ being Ctrl, and pressing Ctrl[ will usually get you the equivalent of pressing Esc.

The Ubuntu ASCII manpage is useful in visualising the mapping:

010   8     08    BS  '\b' (backspace)        110   72    48    H
011   9     09    HT  '\t' (horizontal tab)   111   73    49    I
012   10    0A    LF  '\n' (new line)         112   74    4A    J
013   11    0B    VT  '\v' (vertical tab)     113   75    4B    K
014   12    0C    FF  '\f' (form feed)        114   76    4C    L
015   13    0D    CR  '\r' (carriage ret)     115   77    4D    M
...
033   27    1B    ESC (escape)                133   91    5B    [

The Shift in this case has no effect, Vim sees the same thing as when pressing CtrlV. Try CtrlVCtrlV and CtrlVCtrlShiftV.