What's the meaning of 'inoremap' in vimrc

后端 未结 3 1226
清歌不尽
清歌不尽 2020-12-24 13:14

There is a line below in vimrc example file

inoremap Ctrl-u Ctrl-G u Ctrl-u

What\'s the meaning of inoremap and w

相关标签:
3条回答
  • 2020-12-24 13:31

    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.

    0 讨论(0)
  • 2020-12-24 13:35

    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.

    0 讨论(0)
  • 2020-12-24 13:47

    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!

    0 讨论(0)
提交回复
热议问题