VIM: Conditional Key Mapping

In Vim, I want to have a conditional operator in key mapping.

If the cursor is at the beginning of the line, I want this mapping:

imap <F1> <ESC>:syntax sync fromstart<CR>i

but otherwise it is a match (the only difference is the final character)

imap <F1> <ESC>:syntax sync fromstart<CR>a

In the second display, the cursor does not return to the right place if this mapping is performed when the cursor is at the beginning of the line (when we return to insert mode with a )

I am trying to find a solution to this particular problem, but also want to know if you can really put a conditional expression in the Vim key combination.

Thank!

+4
source share
1 answer

: , . , <expr>. vim wiki:

inoremap <expr> <Esc>      pumvisible() ? "\<C-e>" : "\<Esc>"

Esc C-e, Esc , pumvisible() true false. ( ) , , .

, , .

. <Esc> <C-o>, a i.

imap <F1> <C-o>:syntax sync fromstart<CR>

C-o , . , , .

+5

Source: https://habr.com/ru/post/1688965/


All Articles