Piping Shell Output to an emacs Buffer

2. September 2020

Every few months, I see someone on #emacs mention new eshell functionality, or re-read amebevar’s excellent post on using it as their main shell and am inspired to give it another try. I dream of replacing long pipelines of sed and awk invocations with simple elisp, of experiencing the best parts of Plan9’s UX, seamlessly redirecting output to buffers, and being freed from the tyranny of readline‘s limited vi bindings by the wonderful evil!

Sadly, as a dyed-in-the-wool bash user who’s committed more scripting knowledge to memory than is healthy, I always come back to shell-mode. Without fail, the feature I miss most from my dalliances with eshell is piping command output into a buffer. With this alias, you can have the best of both worlds.

alias eless='(f=$(mktemp "/tmp/pipe.XXX"); cat > $f; emacsclient $f; rm -v $f)'

Invoke it by simply piping to it: cat some-big-file.txt | eless. When you kill the buffer with C-x # your emacsclient session will end, returning you to your shell buffer, and the temp file created will be deleted.