The :map
command takes :help key-notation
, but not variables. Vimscript is evaluated exactly like the Ex commands typed in the :
command-line. There were no variables in ex
, so there's no way to specify them. When typing a command interactively, you'd probably use <C-R>=
to insert variable contents:
:sleep <C-R>=timetowait<CR>m<CR>
... but in a script, :execute
must be used. All the literal parts of the Ex command must be quoted (single or double quotes), and then concatenated with the variables:
execute 'sleep' timetowait . 'm'
Your example
Here's the variable interpolation applied to your example (using a script-local variable):
function! Meta(key) " define if using alt(it works in neovim) or leader key. Used below for mapping only.
if has('nvim')
let s:meta = "<A-" . a:key . ">"
else
let s:meta = "<leader>" . a:key
endif
endfunction
call Meta('j')
execute 'nnoremap <silent>' . s:meta . ' :set paste<CR>m`o<Esc>``:set nopaste<CR>'
However, I would change your function to return the resulting key, so that you save the separate function invocation and variable:
function! Meta(key) " define if using alt(it works in neovim) or leader key.
" for neovim testing and changing habits
if has('nvim')
return "<A-" . a:key . ">"
else
return "<leader>" . a:key
endif
endfunction
execute 'nnoremap <silent>' . Meta('j') . ' :set paste<CR>m`o<Esc>``:set nopaste<CR>'
execute 'nnoremap <silent>' . Meta('k') . ' :set paste<CR>m`O<Esc>``:set nopaste<CR>'