热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在linux上管道输入到sublimetext?-Howtopipeinputtosublimetextonlinux?

HowdoIreceivetextfromstdinintosublimetexteditor?Withvimitworkslikethis:如何从stdin接收文本到

How do I receive text from stdin into sublimetext editor? With vim it works like this:

如何从stdin接收文本到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技巧可以使用吗?

3 个解决方案

#1


11  

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中打开文件,然后删除该文件。我在bin目录中创建了一个名为tosubl的脚本,如下所示:

#!/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

#2


10  

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,但你的问题应该是通用的,因为它适用于任何接受文件名作为参数的程序,但拒绝从stdin读取。

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允许您将stdout从一个进程传输到某种临时文件,然后将该文件的名称传递给另一个进程。

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)表单,则应读取作为参数传递的文件以获取列表的输出。

Assuming SomeProcess produces 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

#3


6  

You might find vipe from moreutils useful. If you've set EDITOR='subl --wait', you can simply:

您可能会发现来自moreutils的vipe非常有用。如果您设置了EDITOR ='subl --wait',您可以简单地:

echo 'potato potato potato' | vipe

推荐阅读
author-avatar
欢喜星空物语
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有