How do you run a command from an installed package from init.coffee?


#1

I feel like this should pretty easy to do, but, as the title says, how do you run a registered command from a package you have installed in init.coffee? The situation is I have vim-mode installed, and want to make alt-o map to the command “vim-mode:insert-below-with-newline”, and then do “vim-mode:reset-command-mode”. How would I create a command to do this? Right now I have the following:

    atom.commands.add 'atom-text-editor',
      'custom:newline-no-insert': (event) ->
        atom.commands.dispatch(atom.views.getView(atom.workspace),
          'vim-mode:insert-below-with-newline')
        atom.commands.dispatch(atom.views.getView(atom.workspace),
          'vim-mode:reset-command-mode')

P.S. Pardon my inexperience, I couldn’t find anything on this in the API and I’m (obviously) pretty new to the Atom customization game.


#2

My guess is that it doesn’t work because you’re dispatching the commands on the workspace while vim-mode triggers its commands on text editors.

Maybe you can try this instead:

atom.commands.add 'atom-text-editor',
  "custom:newline-no-insert": (event) ->
    editorElement = atom.views.getView(atom.workspace.getActiveTextEditor())
    atom.commands.dispatch(editorElement, 'vim-mode:insert-below-with-newline')
    atom.commands.dispatch(editorElement, 'vim-mode:reset-command-mode')

#3

Thanks man, it worked!


#4

That totally worked.

In my case I wanted to built a keymap that allowed me to save and exit from insert mode (using the in vim-mode package) by pressing j-k.

I did it with the following:

// in the init.coffee
atom.commands.add 'atom-text-editor',
  'custom:save-and-normal-mode': (event) ->
    editor = atom.workspace.getActivePane()
    editor.saveActiveItem()
    editorElement = atom.views.getView(atom.workspace.getActiveTextEditor())
    atom.commands.dispatch(editorElement, 'vim-mode:activate-normal-mode')

// in the keymap.cson 
'atom-text-editor.vim-mode.insert-mode':
  'j k': 'custom:save-and-normal-mode'