mik*_*996 21 zsh terminal output
我经常使用find
或locate
来查找路径。
(~) locate foobar.mmpz
/home/progo/lmms/projects/foobar.mmpz
Run Code Online (Sandbox Code Playgroud)
下一步通常是打开或以其他方式操作文件。在像上面这样的快乐情况下,我可以这样做:
(~) ls `!!`
ls `locate foobar.mmpz`
/home/progo/lmms/projects/foobar.mmpz
Run Code Online (Sandbox Code Playgroud)
但是当有很多输出行时,没有人会太高兴,其中一些可能不是路径或其他类似的东西。此外,重新运行可能浪费的命令也不是那么优雅。
有没有办法连接 zsh 以将标准输出存储到数组中以供以后操作?毕竟,将流重定向到用户是 shell 的工作。我认为它可以将前 N 行和最后 N 行存储在变量中以供以后立即使用,例如$?
和其他。
好的,这很酷:https : //unix.stackexchange.com/a/59704/5674。我现在询问 zsh 专有技术(并将代码移植到 zsh)以在每个运行行之后装配这种捕获。
There is no feature to capture the output from the screen on most terminal emulators. I seem to recall the author of xterm (the “reference” terminal emulator) stating that it would be difficult to implement. Even if that was possible, the shell would have to keep track of where the last prompt had been.
So you won't escape having to run the command again, unless you use a terminal-specific, manual mechanism such as copy-pasting with the mouse in xterm or with the keyboard in Screen.
It would be highly impractical for the shell to automatically capture the output of commands, because it cannot distinguish between commands that have complex terminal and user interactions from commands that simply output printable characters.
You can rerun the command and capture its output. There are various ways to do each. To rerun the command, you can use:
!!
history substitution — most convenient to type;fc -e -
, which can be used in a function.To capture the output, you can use command substitution, or a function like the following:
K () {
lines=("${(f@)$(cat)}")
}
!! |K
Run Code Online (Sandbox Code Playgroud)
This sets the lines
array to the output of the command that's piped into it.
这是将最后一行输出放入名为 的变量中的第一部分$lastline
。
precmd () {
exec 2>&- >&-
lastline=$(tail -1 ~/.command.out)
sleep 0.1 # TODO: synchronize better
exec > /dev/tty 2>&1
}
preexec() {
exec > >(tee ~/.command.out&)
}
Run Code Online (Sandbox Code Playgroud)
这使用 zsh 的preexec
钩子来运行exec
以tee
存储命令的 stdout 的副本,然后用于precmd
读取存储的输出并将 stdout 恢复为仅用于显示提示的终端。
但它仍然需要一些工作。例如,由于 stdout 不再是终端,因此vim
和等程序less
无法正常工作。
这些问题中有一些有用的相关信息:
归档时间: |
|
查看次数: |
14178 次 |
最近记录: |