Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pipe content in Emacs' buffer to external program, and print the result?

Tags:

bash

emacs

elisp

How to pipe the selected content in Emacs' buffer to external bash script and then print its output? The script can read data from pipe.

like image 763
kuanyui Avatar asked Mar 02 '12 09:03

kuanyui


People also ask

How do I switch buffers in Emacs?

To move between the buffers, type C-x b. Emacs shows you a default buffer name. Press Enter if that's the buffer you want, or type the first few characters of the correct buffer name and press Tab. Emacs fills in the rest of the name.

How to execute shell commands in Emacs?

You can execute an external shell command from within Emacs using ` M-! ' ( 'shell-command' ). The output from the shell command is displayed in the minibuffer or in a separate buffer, depending on the output size. When used with a prefix argument (e.g, ` C-u M-!

How do I run Emacs?

To execute a file of Emacs Lisp code, use M-x load-file . This command reads a file name using the minibuffer and then executes the contents of that file as Lisp code. It is not necessary to visit the file first; in any case, this command reads the file as found on disk, not text in an Emacs buffer.


1 Answers

Use shell-command-on-region which is bound to M-|

e.g.:
M-| sort | uniq -c RET

With a prefix argument, the region is replaced by the output of the shell command (which effectively adds all shell commands to Emacs' editing toolkit; very useful when you know how to do something outside of Emacs, but don't know an equivalent native function).

C-uM-| sort | uniq -c RET

like image 106
phils Avatar answered Oct 12 '22 02:10

phils