I want to create a function for a mapping. The key for the mapping is configured through a variable. How can I escape the key in the return statement so it works? This works:
let g:some_key = "<Up>"
function! <SID>TestFun()
" doing something
return "\<Up>"
endfunction
noremap <expr> g:some_key <SID>TestFun()
But I want something likes this
let g:some_key = "<Up>"
function! <SID>TestFun()
" doing something
return "\".g:some_key
endfunction
noremap <expr> g:some_key <SID>TestFun()
Right now my solution was creating a new variable holding the escaped key like this
exec 'let s:escaped_key = "\'.g:some_key.'"'
but I'm there's a better way to do it.
EDIT: The method I'm looking for should also work for a function called using <C-R>=<SID>SomeFunction()<CR>
in a imap
fnameescape()
function ? If I store "<Up>" inside theg:some_key
variable then type :echo fnameescape(g:some_key)
, the output is "\<Up>".<C-R>=<SID>SomeFunction()<CR>
inimap
(and in this casefnameescape()
doesn't work. Edited the question accordingly. Thank you for your help