45

There is a line below in vimrc example file

inoremap Ctrl-u Ctrl-G u Ctrl-u

What's the meaning of inoremap and what's the function of this line?

sarnold
  • 102,305
  • 22
  • 181
  • 238
Aaron
  • 1,274
  • 3
  • 14
  • 18

3 Answers3

32

For more on why the command has such a bizarre name see this excellent description between the difference between map and noremap. Really good to know!

To summarise that article, here's a choice quote:

One downside of the *map commands is the danger of recursing...

Vim offers another set of mapping commands that will not take mappings into account when they perform their actions.

So noremap came about to avoid horrible recursion of mappings like

:nmap dd O<esc>jddk

where the dd in the right-hand side of the map recurses back to the left-hand side definition of the map, and Vim gets stuck in an infinite loop!

LondonRob
  • 73,083
  • 37
  • 144
  • 201
29

The vim :help inoremap is very poetic about this:

:ino[remap] {lhs} {rhs}         mapmode-i             :ino :inoremap
:ln[oremap] {lhs} {rhs}         mapmode-l             :ln  :lnoremap
:cno[remap] {lhs} {rhs}         mapmode-c             :cno :cnoremap
                    Map the key sequence {lhs} to {rhs} for the modes
                    where the map command applies.  Disallow mapping of
                    {rhs}, to avoid nested and recursive mappings.  Often
                    used to redefine a command.  {not in Vi}

Thus it makes some insert-mode mappings for ^U that show the filename (^G, undo the most recent change (u), and scrolls the buffer upwards by half a screen (^U).

I have no idea why someone would want this specific sequence of commands, except to demonstrate the inoremap feature -- the ^U at the refers to the meaning the command had when the definition was created, rather than calling back into the redefined ^U mapping.

sarnold
  • 102,305
  • 22
  • 181
  • 238
  • 9
    I was looking at the meaning of inoremap. (insert, no recursion map), when I bumped into this post. – Leo Ufimtsev Aug 08 '18 at 13:40
  • Your explanation of the mapping is incorrect. u and have specific meanings in insert mode. u actually breaks the undo chain, and then clears the current line. – ParadoxFox Apr 26 '22 at 07:26
7

I also wondered about this. See http://vim.wikia.com/wiki/Mapping_keys_in_Vim_-Tutorial(Part_1)#Insert_mode_maps :

Insert mode maps

To map keys that work only in the insert and replace modes, use the 'imap' or 'inoremap' command.

Example: The following command maps to insert the directory name of the current buffer:

:inoremap <F2> <C-R>=expand('%:p:h')<CR>

To display the currently defined insert mode maps, use the 'imap' command without any argument:

:imap

To remove a keymap from insert mode, use the ':iunmap' command. For example, the following command removes the insert mode map for .

:iunmap <F2>

As printable keys insert a character in the current buffer in insert mode, you should use non-printable keys to create insert mode maps. Some examples for non-printable keys include the function keys , keys prefixed with the Ctrl or Alt key.

[snip]


So, for example, in my ~/.vimrc I have

inoremap jk <ESC>
inoremap jj <Esc>

which when pressed in Insert mode return me to Normal mode.

Victoria Stuart
  • 4,610
  • 2
  • 44
  • 37