Adding a key binding only when all tabs are closed


#1

Say I have the vim mode installed, and hence something like the following:

'atom-text-editor.vim-mode.normal-mode':
  ', c k': 'application:open-your-keymap'

That’s all well and good, and works as expected. However, this will not work if all of the tabs are closed and there are no tabs (and hence, I suppose, no vim normal mode). How do I make this binding available when I am in normal mode or when there are no open tabs, but no other situations?


#2

The keybindings match based on CSS selectors. I’m not sure if it is possible to say “select this element only if it does not have a child element X” … I’m Not a CSS Guy™ :grinning: But if it is possible, then I would use that construct on the atom-workspace element.


#3

I think the best way is to have the key binding alwais available, but direct it to a function of your own that check for those criteria before executing open-your-keymap


#4

Unfortunately, the only way I can make it work is via adding a user-defined function, but that seems really problematic for all sorts of reasons. I’m definitely not versed at all in CSS, but I cannot seem to make this work via keymap.cson alone.


#5

I think the state of keybinding is that it’s contextual but not conditional.
I pointed you to the direction of custom function because css mostly exist as reference to parent state.

I do agree that the chosen keyboard shortcut looks somewhat problematic may conflict with normal typing.
Although it may be consistent with vim keyboard navigation mode ( normal mode ).

I’ve seen package register and un-register keyboard binding on the fly, maybe the cleanest way is to listen to tabs events and resister your command as needed. Other user of vim mode may be interested in this too. Maybe you can open a PR there.


#6

Ah, the idea is that I listen and see if the vim mode is in insert mode, and if so, un-register (almost) all mappings, or something like that. A very good suggestion I didn’t think of. For now I’ve set up a much simpler solution (workaround) where I don’t really allow an all tab close: a new file is created if there are no tabs. That’s actually fairly consistent with vim and works for me personally.