[英]Create temporary file with specific extension and open it in VS Code
I'm trying to pipe some output on bash stdout
into a new temporary file and open this file using VS Code. 我正在尝试将bash
stdout
上的一些输出传递到新的临时文件中,并使用VS Code打开此文件。
The file content might not always be plain text but also could be yaml
and I want to create the temp file with yaml
extension so VS Code uses the correct language mode. 文件内容可能并不总是纯文本,也可能是
yaml
,我想创建带有yaml
扩展名的临时文件,因此VS Code使用正确的语言模式。
I tried this but I end up with a file having the txt
in VS Code. 我尝试了这个,但最终得到了一个在VS Code中具有
txt
的文件。
cat yaml > $(mktemp $TMPDIR$(uuidgen).yaml) | code -
cat yaml
is just an example for a command which might write yaml
content to stdout
. cat yaml
只是命令的一个示例,该命令可能会将yaml
内容写入stdout
。
mktemp --suffix=.yaml
would be the way to go but this isn't supported on macOS. mktemp --suffix=.yaml
是mktemp --suffix=.yaml
的方法,但macOS不支持。
I found a solution: 我找到了解决方案:
temp=$(mktemp $TMPDIR.XXXXXXXXXXXX.$(uuidgen).yaml) && ls -la > $temp && code $temp
Of course there might be better ones and I'm looking forward to it. 当然,可能会有更好的选择,我很期待。
You can do it this way: 您可以这样操作:
TMPDIR='/tmp' # Give a value to TMPDIR for the purpose of this answer
mktemp "${TMPDIR}/XXXXXXXXXXXX_$(uuidgen).yaml" |
xargs -n 1 -I {} -- \
code --file-uri "file://{}"
mktemp "${TMPDIR}/XXXXXXXXXXXX_$(uuidgen).yaml"
: mktemp "${TMPDIR}/XXXXXXXXXXXX_$(uuidgen).yaml"
: $TMPDIR
with name composed of: $TMPDIR
,其名称包括:
X
, X
, _
, _
, uuidgen
command, uuidgen
命令生成的随机UUID, .yaml
extension. .yaml
扩展名结尾。 | xargs -n 1 -I {}
| xargs -n 1 -I {}
: | xargs -n 1 -I {}
: xargs
, which will convert it into an argument: xargs
,它将把它转换为参数:
-n 1
: One argument for each command -n 1
:每个命令一个参数 -I {}
: Replace placeholder string {}
with the argument -I {}
:用参数替换占位符字符串{}
--
: End of the xargs
options. --
: xargs
选项的结尾。 code --file-uri "file://{}"
: code --file-uri "file://{}"
: code
is invoked by xargs
. code
由xargs
调用。
--file-uri <uri>
Opens a window with given file uri(s) --file-uri <uri>
使用给定的文件uri打开一个窗口 "file://{}"
: The uri for the temporary file just created. "file://{}"
:刚刚创建的临时文件的uri。 xargs
will replace {}
by the temporary file path. xargs
将用临时文件路径替换{}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.