zsh: using “less -R” as READNULLCMD

浪子不回头ぞ 提交于 2020-02-25 06:33:12

问题


Now, I'm pretty sure of the limitation here. But let's step back.

The simple statement

 READNULLCMD="less -R"

doesn't work, generating the following error:

$ <basic.tex                                                                            
zsh: command not found: less -R

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

So, what's the best way around this, without setting SH_WORD_SPLIT (which I don't want 99% of the time)?

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

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.

Anybody have any better ideas?


回答1:


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

does not translate into

less $LESS foo

but rather to something like

cat foo | less $LESS

or, perhaps

cat foo $LESSOPEN | less $LESS

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".

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.




回答2:


export LESS=… may be a good solution exclusively for less and if you want such behavior the default in all cases, but if you want more generic one then you can use functions:

function _-readnullcmd()
{
    less -R
}
READNULLCMD=_-readnullcmd

(_- or readnullcmd have no special meaning just the former never appears in any distributed zsh script and the latter indicates the purpose of the function).




回答3:


Set the $LESS env var to the options you always want to have in less.

So don't touch READNULLCMD and use export LESS="R" (and other options you want) in your zshrc.



来源:https://stackoverflow.com/questions/14053184/zsh-using-less-r-as-readnullcmd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!