I'm referring to plugins like gundo and nerdtree, which offer a whole lot of key bindings only when the cursor is their window.

Therefore, I was wandering if there's a common or preferred way to accoplish something like this.

The intuitive way, to me, seems pretty obvious: create a autocmd that is triggered when the cursor moves into that window, and another one when it moves away from that windows. The two autocmds should activate the plugin-specific mappings and revert the original mappings/functionalities respectively.

However, I can't see how the latter can be done without having to write a list of "mappings to be recovered or removed".

The motivation for this question is that I'm considering writing a small plugin to make all window-related actions (like moving them, moving across them, opening them, closing them, ...) easier to perform, as described here.

  • 3
    Isn't it simply with the <buffer> argument to nnoremap ? :h :map-local – perelo Oct 18 '20 at 11:53
  • It seems like the plugins you refer to are doing this here and here. They are not window-specific but buffer-specific mappings. – perelo Oct 18 '20 at 12:03
  • @perelo, so it seems they only re-map what they need, without deactivate any other mapping or key for the sake of it, right? As in, if the plugin doesn't need to remap j to do something plugin-specific, then j will be left with its normal meaning, and not mapped to <nop>. – Enlico Oct 18 '20 at 15:20
  • 1
    yes exactly. Global mappings are still available when buffer ones haven't overridden them. For your question, ie wriring window-specific mappings, you are right. I think we need to keep a list of current window mappings and use WinEnter and WinLeave to load and unload them. – perelo Oct 18 '20 at 16:20

Your Answer

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

Browse other questions tagged or ask your own question.