bash 如何在 Linux 上将输入通过管道传输到 sublimetext?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21125094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pipe input to sublimetext on linux?
提问by wim
How do I receive text from stdin into sublimetext editor? With vim it works like this:
如何从标准输入接收文本到 sublimetext 编辑器?使用 vim 它是这样工作的:
echo "potato potato potato" | vim -
The same thing works with gedit, creating a new document with the contents.
同样的事情适用于 gedit,创建一个包含内容的新文档。
Sublimetext seems to just start editing a file called "-" in a new tab, is there some other shell trick which could work?
Sublimetext 似乎只是开始在新选项卡中编辑一个名为“-”的文件,是否还有其他一些可行的 shell 技巧?
回答by tylerl
The simplest solution if your sublime installation doesn't support opening STDIN (e.g. current version on Linux) is to dump the output to a temporary file, open the file in sublime, and then delete the file. I created a script called tosubl
in my bin directory as follows:
如果您的 sublime 安装不支持打开 STDIN(例如 Linux 上的当前版本),最简单的解决方案是将输出转储到一个临时文件,在 sublime 中打开该文件,然后删除该文件。我tosubl
在我的 bin 目录中创建了一个脚本,如下所示:
#!/bin/bash
TMPDIR=${TMPDIR:-/tmp} # default to /tmp if TMPDIR isn't set
F=$(mktemp $TMPDIR/tosubl-XXXXXXXX)
cat >| $F # use >| instead of > if you set noclobber in bash
subl $F
sleep .3 # give subl a little time to open the file
rm -f $F # file will be deleted as soon as subl closes it
Then you can use it by doing something like this:
然后您可以通过执行以下操作来使用它:
$ ifconfig | tosubl
回答by Ruud Helderman
I don't know Sublime Text, but your problem should be generic in that it applies to any program that does accept a filename as argument, but refuses to read from stdin.
我不知道 Sublime Text,但您的问题应该是通用的,因为它适用于任何接受文件名作为参数但拒绝从标准输入读取的程序。
Fortunately, Bash allows you to pipe stdout from one process into some kind of temporary file, then pass the name of that file to another process.
幸运的是,Bash 允许您将标准输出从一个进程通过管道传输到某种临时文件中,然后将该文件的名称传递给另一个进程。
From man bash
:
来自man bash
:
Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files. It takes the form of <(list) or >(list). The process list is run with its input or output connected to a FIFO or some file in /dev/fd. The name of this file is passed as an argument to the current command as the result of the expansion. If the >(list) form is used, writing to the file will provide input for list. If the <(list) form is used, the file passed as an argument should be read to obtain the output of list.
在支持命名管道 (FIFO) 或命名打开文件的 /dev/fd 方法的系统上支持进程替换。它采用 <(list) 或 >(list) 的形式。进程列表运行时其输入或输出连接到 FIFO 或 /dev/fd 中的某个文件。该文件的名称作为扩展的结果作为参数传递给当前命令。如果使用 >(list) 形式,写入文件将为列表提供输入。如果使用 <(list) 形式,则应读取作为参数传递的文件以获得 list 的输出。
Assuming SomeProcessproduces output that you would like to capture in your editor:
假设SomeProcess生成您想在编辑器中捕获的输出:
sublimetext <(SomeProcess)
or:
或者:
SomeProcess | sublimetext <(cat)
If you think you will be typing this in by hand a lot, then you may want to put sublimetext <(cat)
into a shell script or alias.
如果您认为您会经常手动输入这些内容,那么您可能想要放入sublimetext <(cat)
一个 shell 脚本或别名。
Just in case your OS does not support process substitution, then you can always specify a temporary file yourself of course:
以防万一你的操作系统不支持进程替换,那么你当然可以自己指定一个临时文件:
SomeProcess > /tmp/myoutput
sublimetext /tmp/myoutput
回答by ?ndrük
You might find vipe
from moreutilsuseful. If you've setEDITOR='subl --wait'
, you can simply:
您可能会发现vipe
从moreutils有用。如果你已经设置EDITOR='subl --wait'
,你可以简单地:
echo 'potato potato potato' | vipe
回答by Rauno56
Piggybacking on https://stackoverflow.com/a/31035834/2429912a bit, since for me it does 90% but not all of it:
在https://stackoverflow.com/a/31035834/2429912 上捎带一点,因为对我来说它做了 90% 但不是全部:
Using a temporary file is a way that can be used with virtually any editor. Even better if the editor supports waiting until the file is closed(sublime -w
for Sublime Text) you can actually edit it on the fly, which makes it more versatile. To do that you need to alter the script @tylerl provided - script named tosubl
in your PATH:
使用临时文件是一种几乎可以与任何编辑器一起使用的方法。如果编辑器支持等待文件关闭(sublime -w
对于 Sublime Text)则更好,您实际上可以即时编辑它,这使其更加通用。为此,您需要更改提供的脚本 @tylerl -tosubl
在您的 PATH 中命名的脚本:
#!/bin/bash
TMPDIR=${TMPDIR:-/tmp} # default to /tmp if TMPDIR isn't set
F=$(mktemp $TMPDIR/tosubl-XXXXXXXX)
cat >| $F # use >| instead of > if you set noclobber in bash
subl -w $F # waits until the file is closed
cat $F # prints out the contents of the file to stdout
rm -f $F # clean up the file
Now running echo "hello" | tosubl > local.file
would open the output of the first script in Sublime and once you have closed it, save it to local.file
.
现在运行echo "hello" | tosubl > local.file
将在 Sublime 中打开第一个脚本的输出,一旦你关闭它,将它保存到local.file
.
回答by danmou
If you want to display colored text with ANSI escape sequences (a terminal buffer for example), you can install the package ANSIescapeand use the following command:
如果要显示带有 ANSI 转义序列(例如终端缓冲区)的彩色文本,可以安装ANSIescape包并使用以下命令:
F=$(mktemp); cat > $F; subl $F; subl --command ansi; sleep .5; rm -f $F
F=$(mktemp); cat > $F; subl $F; subl --command ansi; sleep .5; rm -f $F