zsh:使用“less -R";作为 READNULLCMD [英] zsh: using "less -R" as READNULLCMD
问题描述
现在,我很确定这里的限制.但让我们退后一步.
Now, I'm pretty sure of the limitation here. But let's step back.
简单的语句
READNULLCMD="less -R"
不起作用,产生以下错误:
doesn't work, generating the following error:
$ <basic.tex
zsh: command not found: less -R
好的.很确定这是因为,默认情况下, zsh 不会在每个空间拆分字符串变量.无论 zsh 在何处使用此变量,它都会在应该使用 ${=READNULLCMD} 的地方使用 $READNULLCMD,以确保选项参数通过普通空格与命令正确分隔.早在 1996 年就看到这个讨论(!):http://www.zsh.org/mla/users/1996/msg00299.html
OK. Pretty sure this is because, by default, zsh doesn't split string variables at every space. Wherever zsh is using this variable, it's using $READNULLCMD where it should be using ${=READNULLCMD}, to ensure the option argument is properly separated from the command by a normal space. See this discussion from way back in 1996(!): http://www.zsh.org/mla/users/1996/msg00299.html
那么,在不设置 SH_WORD_SPLIT(我 99% 的情况下都不想要)的情况下,解决此问题的最佳方法是什么?
So, what's the best way around this, without setting SH_WORD_SPLIT (which I don't want 99% of the time)?
到目前为止,我最好的想法是将 READNULLCMD 分配给一个简单的 zsh 脚本,该脚本只在 STDIN 上调用less -R".例如
So far, my best idea is assigning READNULLCMD to a simple zsh script which just calls "less -R" on STDIN. e.g.
#!/opt/local/bin/zsh
less -R /dev/stdin
不幸的是,这似乎是一个非入门者,因为出于某种原因,这种方式很少使用,错过了/dev/stdin 输入的前几行.
Unfortunately this seems to be a non-starter as less used in this fashion for some reason misses the first few lines on input from /dev/stdin.
有人有更好的想法吗?
推荐答案
问题不在于 less 不读取其环境变量(LESS 或 LESSOPEN).问题是 READNULLCMD 没有像您想象的那样被调用.
The problem is not that less doesn't read its environment variables (LESS or LESSOPEN). The problem is that the READNULLCMD is not invoked as you might think.
<foo
不会翻译成
less $LESS foo
而是类似于
cat foo | less $LESS
或者,也许
cat foo $LESSOPEN | less $LESS
我猜你(像我一样)想使用 -R 来获得语法着色(通过在 LESSOPEN 中使用 src-hilite-lesspipe.sh,而后者又使用source-highlight"实用程序).后两种调用方式的问题在于 src-hilite-lesspipe.sh(嵌入在 $LESSOPEN 中)不会收到文件名,因此它无法推断文件类型(通过 --infer-lang源代码高亮"选项).如果没有文件名后缀,source-highlight"将恢复为no highlighting".
I guess that you (like me) want to use -R to obtain syntax coloring (by using src-hilite-lesspipe.sh in LESSOPEN, which in turn uses the "source-highlight" utility). The problem with the latter two styles of invocation is that src-hilite-lesspipe.sh (embedded in $LESSOPEN) will not receive a filename, and hence it will not be able to deduce the file type (via the --infer-lang option to "source-highligt"). Without a filename suffix, "source-highlight" will revert to "no highlighting".
您可以在 READNULLCMD 中获得语法着色,但以一种相当无用的方式.这是通过 --lang-def 选项明确指定语言.但是,您将与source-highlight"一样没有任何线索,因为当数据通过管道匿名传递时没有文件名.也许有一种方法可以执行动态启发式解析器并根据内容对其进行推断,但是您肯定已经离开了这个小练习.
You can obtain syntax coloring in READNULLCMD, but in a rather useless way. This by specifying the language explicitly via the --lang-def option. However, you'll have as little clue as "source-higlight", since the there's no file name when the data is passed anonymously through the pipe. Maybe there's a way to do a on-the-fly heuristic parser and deduce it by contents, but then you've for sure left this little exercise.
这篇关于zsh:使用“less -R";作为 READNULLCMD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!