Pipe emacs shell output to a new buffer

2020-08-10 02:04发布

For example, I want to be able to type something like:

$ git diff | tempbuffer

and have the diff opened in a new, unsaved buffer.

标签: shell emacs
5条回答
beautiful°
2楼-- · 2020-08-10 02:26

I made a package (e-sink)using information from the emacs wiki as a starting point. It works as you described and "tail"s the ouput instead of waiting until the process finishes to display everything.

查看更多
smile是对你的礼貌
3楼-- · 2020-08-10 02:27

My personal preference is for something you can type in Bash without having to manage any files:

git diff | (f=$(mktemp); cat > $f; emacsclient $f; rm -v $f)

emacsclient waits for you to be finished with the buffer before Bash deletes the temporary file.

I would use M-! (phils's answer) if I was starting the shell command from scratch and the above (which is similar to Sean's answer) if I was 'in the middle of something' in the shell and then decided 'I want to pipe this to Emacs'.

查看更多
Fickle 薄情
4楼-- · 2020-08-10 02:31

You can just use M-! -- it will run the command within the same cwd as your shell buffer, and output the results to a *Shell Command Output* buffer.

Note that if the results are brief, that buffer will not be raised and the output will be copied to the echo area; however the buffer is still used and available. C-hf shell-command RET has details of what constitutes "brief" output:

If the output is short enough to display in the echo area (determined by the variable max-mini-window-height if resize-mini-windows is non-nil), it is shown there. Otherwise, the buffer containing the output is displayed.

查看更多
Luminary・发光体
5楼-- · 2020-08-10 02:36

If you use eshell you can redirect output to a buffer, e.g.

 print foo > #<buffer bar>

which creates a new buffer bar with the content 'foo'. For further details, see the Emacswiki at http://www.emacswiki.org/emacs/EshellRedirection.

查看更多
别忘想泡老子
6楼-- · 2020-08-10 02:42

Unfortunately emacsclient doesn't read its standard input, so you need some kind of wrapper. Here's a Bash shell function that works for me:

tempbuffer() {
  perl -MFile::Temp -MFile::Copy -e \
  'copy *STDIN, $file = File::Temp->new; system "emacsclient", $file';
}
查看更多
登录 后发表回答