I know that in vim
, map <expr>
allows us to map a key sequence to a new sequence as specified by the return value of an expression. For example:
function Wrap()
if getline('.') == ''
" can I return a <Plug> here?
return "Ends an empty line\<cr>"
else
return "\<cr>"
endif
endfunction
inoremap <expr> <cr> Wrap()
In the expression, \<cr>
means to return the <cr>
key.
We can also have <Plug>
maps like:
inoremap <Plug>empty "Ends an empty line\<cr>"
imap <cr> <Plug>empty
My question is: if I want to return the <Plug>empty
map only when cursor is in an empty line like in the first example, what should I do?
As @Mass suggested, the solution is:
inoremap <Plug>empty "Ends an empty line\<cr>"
function Wrap()
if getline('.') == ''
return "\<Plug>empty"
else
return "\<cr>"
endif
endfunction
imap <expr> <cr> Wrap()
The last line must use imap
instead of inoremap
.