如何在映射中访问脚本变量?

How to access script-variable in mapping?

我想做以下行为

let s:cmd = “echo ‘here is a long command’”

map aa :execute s:cmd<cr>
“ error, mapping cannot access script variable 

map bb :execute <SID>cmd<cr>
“ error no variable named <SNR>…cmd 

如何访问映射中的脚本变量?

您可以提供脚本本地函数,然后通过该函数访问变量。

let s:var = 'foo'

function! s:get(key) abort
  return get(s:, a:key)
endfunction

nnoremap <silent> µ :<c-u>echo <sid>get('var')<cr>