3

Is it possible to somehow edit in-memory text in emacs? (I.e. without going via the file system.)

I'm writing a script in which I need to edit the output of a command and then process the edited text in another command.

I'm thinking of something like

command1 | emacs --edit-std-in-and-write-to-stdout | command2

Or, alternatively something like

TEXT=$(command1)
emacs --edit-this $TEXT | command2
aioobe
  • 284
  • 1
  • 18

1 Answers1

1

Emacs won't read standard input unless it's running in batch mode. Instead, you can use a command-line option to pass a Lisp form which Emacs will evaluate immediately after initialization:

emacs --eval '(shell-command "command1")'

This will execute command1 and capture its output into a buffer named *Shell Command Output*, which you can then rename, edit, &c. to taste.

Once done, you can pass the result to command2 by means of C-x h M-| command2 RET; C-x h calls mark-whole-buffer so that the region encompasses the entirety of your edited text, and M-| calls shell-command-on-region to execute command2 and supply contents of the region on standard input. (If you've rebound either of those chords, just call the relevant function directly via M-x function-name RET instead; as with all interactive Emacs Lisp functions, they will use the minibuffer to prompt for the arguments they need.)

As with shell-command, shell-command-on-region will capture standard output to the *Shell Command Output* buffer, replacing whatever happens to be there; if you want to keep around the edited output of command1, rename the buffer before invoking command2.)

One further note: you may find it useful to know that both shell-command and shell-command-on-region take an optional output buffer argument, as either a buffer or a buffer name; if a buffer by the name given does not exist, a new buffer by that name will be created. See the internal documentation (C-h f function-name RET or M-x describe-function RET function-name RET) for more.

Aaron Miller
  • 10,087