Emacs has tonnes of features built in, and is infinitely expandable with emacs lisp, but sometimes there is already a shell command that can do the job you want more easily. You can use M-!
(wich runs shell-command
) to run a shell command with the contents of the current buffer passed to stdin (standard input for the command), or M-|
(which runs shell-command-on-region
) to do the same for the currently selected text in the buffer.
The output of the shell command is then shown as a new buffer in emacs.
As a trivial example, suppose a buffer contained the following, and we wanted to sort the lines (N.B. this is easy to do in emacs)
bbb aaa ddd ccc
Hitting M-!
prompts for a shell command, and we then enter sort
to call the standard unix/linux sort command. We now get a buffer called *Shell Command Output*
containing
aaa bbb ccc ddd
Since I am not the world’s greatest lisp programmer, I use this quite often for things that could probably be accomplished in emacs. For example, I wanted to pull out all of the xmlUrl addresses from a large file that looked like this
<opml version="1.0"> <head> <title>Ben subscriptions in feedly Cloud</title> </head> <body> <outline text="daily" title="daily"> <outline type="rss" text="Information Is Beautiful" title="Information Is Beautiful" xmlUrl="http://feeds.feedburner.com/InformationIsBeautiful" htmlUrl="http://www.informationisbeautiful.net"/> <outline type="rss" text="Planet Emacsen" title="Planet Emacsen" xmlUrl="http://planet.emacsen.org/atom.xml" htmlUrl="http://planet.emacsen.org/"/> <outline type="rss" text="What If?" title="What If?" xmlUrl="http://what-if.xkcd.com/feed.atom"/> </outline> </body> </opml>
I couldn’t come up with a way to do this internally in emacs, but it is easy for me in perl, so I just ran M-!
and then entered
perl -ne '/xmlUrl=(".+?")/; print "$1\n"' | uniq
to get a buffer containing just
"http://feeds.feedburner.com/InformationIsBeautiful" "http://planet.emacsen.org/atom.xml" "http://what-if.xkcd.com/feed.atom"
I’d be curious if anyone has a nice way to do this in emacs alone!