1

I'm trying to set a nmap bind to make the focus change from Window B back to Window A. Both Window IDs are store at the script level: s:win_id_a and s:win_id_b.

The current snippet I've is the following at plugins/my-plugin.vim:

function! s:InitialSetup
  let s:win_id_a = win_getid()
  bot new
  let s:win_id_b = win_getid()
  " bunch of unrelated setups ...
  win_gotoid(s:win_id_b)  " Making sure we are binding the correct win/buff
  map <buffer> c :call win_gotoid(s:win_id_a)<CR>
endfunction

When I tried to trigger the map/bind I got an error: Undefined variable: s:win_id_a.

The question is: Should I "cast" s:win_id_ato a global OR is there another way I can achieve this win_gotoid bind call passing the script level variable?

2
  • 1
    Welcome to Vi and Vim! You understand the context of s: variables is that they’re local to the script (functions and mappings in the *.vim file) where they’re defined, right? Is your :map command defined in the same script that is defining those variables? Please edit your question to add more context on where the variables are defined and where your :map command lives.
    – filbranden
    Jul 16 at 17:07
  • 1
    @filbranden ty for the reply. does this helps adding more context? Jul 16 at 17:31

1 Answer 1

1

The righthand side of a mapping is a series of key presses. That is, it works almost as if you pressed sequentially key "colon", then "c", then "a", then "l", etc.etc. on your keyboard.

Therefore, the righthand side of mapping is not a command and it can't have any script context associated with it. And therefore you can't access script-local variables within it.

Speaking of your case, use window-local variables to store data per window, or global variables when appropriate.

It is also possible to call script-local functions from mapping (and so to get access to script-local variables from inside). But in VimScript local functions are actually "globals" with mangled names, so they are accessible from everywhere, unlike variables.

2
  • You might want to include a reference to :help script-variable in your answer, and perhaps an example of using a script-local function in a mapping to accomplish the objective.
    – filbranden
    Jul 16 at 21:22
  • @Matt thank you for the answer. I decide to resolve using you suggestion of wrapping those routines depending on local scope variables into script scope functions mapped as Editor Commands. Jul 20 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.