简体   繁体   English

当原子启动时如何运行命令

[英]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. 我想在Atom启动时运行各种命令,以便它以我期望的状态打开,而不必每次都手动运行这些命令。

I know init.coffee is run when Atom starts, but I don't know how to run a command from there. 我知道init.coffee是在Atom启动时运行的,但是我不知道如何从那里运行命令。

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更改为所需的package&命令,然后将结果放入init.coffee

package:command is the same syntax you would use in keymap.cson . package:command与您在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 例如: Fuzzy Finder: Toggle File Finder变为fuzzy-finder:toggle-file-finder

atom.views.getView(atom.workspace) is to dispatch the command into the full workspace. atom.views.getView(atom.workspace)将命令分配到整个工作区中。 If you want to target the current text editor instead, try atom.views.getView(atom.workspace.getActiveTextEditor()) . 如果要改为定位当前的文本编辑器,请尝试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 ). 您可以通过在Dev Tools Console中运行它来测试整个事情(使用Window: Toggle Dev ToolsCtrl+Shift+IF12打开它)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM