Search code examples
commandstartupatom-editor

How to run a command when atom starts


I would like to run various commands when Atom starts, so that it opens in the state I expect it to without having to run those commands manually every time.

I know init.coffee is run when Atom starts, but I don't know how to run a command from there.


Solution

  • I finally found the answer here:

    atom.commands.dispatch(atom.views.getView(atom.workspace), 'package:command');

    Just change package:command to your desired package & command, and put the result in your init.coffee.

    package:command is the same syntax you would use in keymap.cson. Basically, it's the name of the package and the name of the command you can find in the command palette, but lowercased and using dashes instead of spaces. ex: Fuzzy Finder: Toggle File Finder becomes fuzzy-finder:toggle-file-finder

    atom.views.getView(atom.workspace) is to dispatch the command into the full workspace. If you want to target the current text editor instead, try atom.views.getView(atom.workspace.getActiveTextEditor()).

    You can test the whole thing by running it in the Dev Tools Console (open it using Window: Toggle Dev Tools, or Ctrl+Shift+I, or F12).

    > atom.commands.dispatch(atom.views.getView(atom.workspace), 'fuzzy-finder:toggle-file-finder');
    Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: Array(1)}