How can I make my normal mode mapping work from within Insert

Mode? Mappings in normal mode can be executed after <Ctrl-O> from insert mode as well but if there are more commands included in the mapping {rhs}, only the first one will be executed in normal mode and the rest of {rhs} will be printed literally in insert mode. One of ways to workaround this problem is to make {rhs} be one command, via wrapping it to the function. For example:

    function GetFontNameOfFirstChar()

    normal! 0

    echo getfontname()

    endfunction

:nmap <F9> :call GetFontNameOfFirstChar()<CR> A more technical and detailed solution to this problem follows and can be found at https://groups.google.com/group/vim_dev/msg/75f1f2dfc00908bb Not every normal mode-mapping is automatically suitable for execution via <Ctrl-O> from within insert mode; you need to explicitly design your mappings for that purpose. The <Ctrl-O> command allows execution of one normal mode command from within insert mode, then returns to insert mode. If a normal mode mapping concatenates multiple normal mode commands, this breaks down in temporary normal mode and literally inserts the second part of the command into the buffer instead. To support execution of normal mode mappings from within insert mode, these strategies can be used: 1) Instead of concatenating multiple normal mode commands, use one :normal mapping:

    :nnoremap  zC :normal! zCVzC

2) Concatenate multiple Ex commands via <Bar> on the rhs:

    :nnoremap zC :call MyMap1()call MyMap2()

3) Shadow normal mode mappings by insert mode mappings that re-enter normal mode, then invoke the normal mode mapping:

    :nnoremap  MyMap2 :call MyMap2()

    :inoremap   <SID>MyMap2 <C-\><C-O><SID>MyMap2</pre>

    :nnoremap   zC <SID>MyMap1<SID>MyMap2</pre>

4) Normal mode mappings that consist of multiple Ex command lines (and where Ex commands cannot be concatenated via <Bar>) replace ‘:<C-U>’ with <SID>NM; the <SID>NM mapping enters normal mode for one ex command line:

    :nnoremap  NM :

    :inoremap  NM :

    :nnoremap   zC <SID>MyMap1<SID>NMcall MyMap2()<CR></pre>

5) If none of the above is possible, at least force normal mode for subsequent commands via <CTRL-\><CTRL-N> to avoid accidental insertion of the remainder of the mapping.

    :nnoremap zC zCVzCzz

Comments (1)


Pepsh Pepshinsky

Let’s see you to :help viminfo-file-name (option -i) :set viminfofile=NONE


Speak Your Mind