vim

Vim: how to change settings 'iminsert' and 'imsearch' in activated search mode immediately?


I am trying to configure

nmap <C-F2> :set iminsert=1 imsearch=1<cr>
imap <C-F2> <C-O><C-F2>

in order to change vim language. It works well in command and insert modes. But it does not work in a search mode. Trying to implement it I wrote a function

function! Call_F2()
    set iminsert=1
    set imsearch=1
    return ""
endfunction

and mapped it with a

cmap <C-F2> <C-R>=Call_F2()<cr>

so I type / and press C-F2, but settings are not applied to the current search mode. Now I have to exit and re-enter search mode in order to see my changes applied.

The question is: how to change settings 'iminsert' and 'imsearch' in activated search mode immediately?

(I know I can use a shortcut C-^ and it works but I would like to map languages to their own shortcuts)


Solution

  • Many thanks for Joanis! I have improved hers/his solution:

    nmap <C-F1> :set iminsert=0 imsearch=0<cr>
    imap <C-F1> <C-O><C-F1>
    cmap <C-F1> <END><SPACE><C-C><C-F1>/<UP><DEL>
    "
    nmap <C-F2> :set iminsert=1 imsearch=1<cr>
    imap <C-F2> <C-O><C-F2>
    cmap <C-F2> <END><SPACE><C-C><C-F2>/<UP><DEL>
    

    the only side-effect I have noticed: the cursor always jumps to the end of search pattern.