Only allowing keybind to be triggered when in command mode (Vim-Mode)


I’ve set ,t to toggle and focus the tree pane. I’m using vim-mode, and I only want the keybinding to work when I’m not in insert mode. Here are the relevant lines from my keymap.cson file:

'.platform-win32, .platform-linux, atom-text-editor.vim-mode:not(.insert-mode)':
  ', t': 'tree-view:toggle-focus'
'.platform-win32, .platform-linux, atom-text-editor.vim-mode:not(.insert-mode)':
  ', t': 'tree-view:toggle'

However, this is not doing what it’s intended to do. When I try to type a comma, I have to press the comma key twice in order to have a single comma appear. I think that if I was able to force the keybinding to only work outside of insert mode, the comma key would work properly, but I’m not sure how to do that. I’ve also tried replacing vim-mode:not(.insert-mode) with vim-mode.command-mode but that did not work either. How could I make the keybinding only work when I’m not in insert mode? Thanks!


Your keybinding applies to any .platform-win32 element, any .platform-linux element and any atom-text-editor.vim-mode:not(.insert-mode) element. Instead you should use

'.platform-win32 atom-text-editor.vim-mode:not(.insert-mode), .platform-linux atom-text-editor.vim-mode:not(.insert-mode)':
  ', t': 'tree-view:toggle'

Also, by using the same selector twice, or the same keybinding for the same selector, the latter overwrites the former. If you want , T to trigger both tree-view:toggle-focus and tree-view:toggle you should add this to your

# create new command my-commands:toggle-tree-view
# that triggers both tree-view:toggle-focus and tree-view:toggle
atom.commands.add 'atom-workspace', 'my-commands:toggle-tree-view', ->
  atom.commands.dispatch 'atom-workspace', 'tree-view:toggle-focus'
  atom.commands.dispatch 'atom-workspace', 'tree-view:toggle'

Then replace the above config with this:

'.platform-win32 atom-text-editor.vim-mode:not(.insert-mode), .platform-linux atom-text-editor.vim-mode:not(.insert-mode)':
  # dispatch the newly created my-commands:toggle-tree-view on ', t'
  ', t': 'my-commands:toggle-tree-view'


That’s interesting that the latter overwrites the former, because my keymap.cson does work both toggle and focus the tree, but when I put in the first snippet of code that you suggested, it no longer works properly. It also doesn’t work when I put the custom command in my and keymap.cson.

I also tried changing not:(.insert-mode) to .command-mode, and switching the order of toggle-focus and toggle in my, but that didn’t work either.

Do you have any other ideas as to how to make the comma key work properly? I don’t have any experience with CoffeeScript, so I’ll do my best to help, but I’m not really sure what to try. Thanks!


atom.commands.dispatch takes an actual DOM element as its first argument. This may work better:

workspaceElement = atom.views.getView(atom.workspace)
atom.commands.add 'atom-workspace', 'my-commands:toggle-tree-view', ->
  atom.commands.dispatch workspaceElement, 'tree-view:toggle-focus'
  atom.commands.dispatch workspaceElement, 'tree-view:toggle'


I just figured out what @leedohm posted, yes you should do that. And you can also probably replace the keybinding with a variant without the platforms, so it looks a little neater:

  ', t': 'my-commands:toggle-tree-view'


Ok, I tried replacing the original suggestion for my to what @leedohm suggested and changing the keybinding to:

  ', t': 'my-commands:toggle-tree-view'

But it still isn’t working. If there is other code in my before the tree-toggling section, do I have to do anything so that the tree-toggling section will work?


Shouldn’t matter where you put it in your, as long as nothing before it crashes your init script :stuck_out_tongue:

I tested the code suggested by @leedohm and me, and it just works on my machine. Are there perhaps any other keybindings interfering? You can check that by opening the keybinding-resolver with Ctrl+. and then when you use your keybindings it will list matching keybindings, and indicate the “winner”


What exactly is it that doesn’t work? If you’re referring to being able to open the tree view with the keybinding, but then being unable to hide it again with the keybinding, that is because the keybinding is scoped to the atom-text-editor.vim-mode:not(.insert-mode), but keybindings only trigger on focused elements. So if you focus the tree view, the keybinding doesn’t trigger anymore because the atom-text-editor.vim-mode is unfocused.

What you can do to solve this is remove the custom command, and let the keybinding trigger tree-view:toggle:

  ', t': 'tree-view:toggle'

The tree view is automatically focused when it is opened. You can unfocus it with escape and then use the , T keybinding to hide the tree view…

but why wouldn’t you then use the escape key to trigger tree-view:toggle on the tree view:

  'escape': 'tree-view:toggle'

or you can also also bind the , T to the tree-view:toggle command on the tree view:

'atom-text-editor.vim-mode:not(.insert-mode), .tree-view':
  ', t': 'tree-view:toggle'

The last one is basically all you need :smile:


Well, that last keybinding made it work properly! Thank you so much! After all that, just needed to add .tree-view…I really appreciate the help.