Package that's runs a cli command with the selected text as its first argument?


#1

Hi community!

I’m interested if there is a package that let’s me run a cli command with the selected text?


#2

I don’t know about any packages but you can accomplish this in your init script (~/.atom/init.coffee). Consider the following example:

{BufferedProcess} = require 'atom'
atom.commands.add 'atom-text-editor', 'custom:execute-command-on-highlighted-text': ->
  selectedText = atom.workspace.getActiveTextEditor().getSelectedText()
  command = 'echo'
  args = [selectedText]
  options = {}
  output = ''
  stdout = (out) ->
    output += out
  exit = (code) ->
    if code isnt 0
      message = "'#{command}' failed to execute sucessfully!"
      options =
        detail: output
        dismissable: true
      atom.notifications.addError message, options
    else
      message = "'#{command}' executed successfully!"
      options =
        detail: output
        dismissable: false
      atom.notifications.addSuccess message, options
  new BufferedProcess {command, args, options, stdout, exit}

This will pass the selected text in your active editor to the echo command and create a notification with its output.


Run local exe in atom
#3

Nice, thanks for the answer


#4

There are packages that claim to run selected code from many languages. You should request support for scripts. They may even have one already. I prefer packages over init code because they are supported.