How do I write a custom command in Atom?

Lee picture Lee · Jun 27, 2014 · Viewed 9.7k times · Source

I want to write a command for Atom that composes two or more pre-existing commands, like "Select Line" and then "Cut". How do I do that?

Answer

Lee picture Lee · Jun 27, 2014

You can add the following code to your init.coffee file:

atom.commands.add 'atom-text-editor', 'custom:cut-line', ->
  editor = atom.workspace.getActiveTextEditor()
  editor.selectLinesContainingCursors()
  editor.cutSelectedText()

You can get the code to execute from the source by searching for strings in the command palette. And once you have a command created, you can map a key to it by editing your keymap.cson file:

'atom-text-editor':
    'alt-cmd-z': 'custom:cut-line'