Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access script-variable in mapping?

Tags:

vim

I want to do following behaviour

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 

How to access the script variable in mapping?

like image 511
Siuol Lee Avatar asked Nov 18 '25 20:11

Siuol Lee


1 Answers

You can provide a script local function and then access to the variable through that function.

let s:var = 'foo'

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

nnoremap <silent> µ :<c-u>echo <sid>get('var')<cr>
like image 151
Luc Hermitte Avatar answered Nov 22 '25 00:11

Luc Hermitte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!