Zsh提示自定义 [英] Zsh Prompt Customization
问题描述
我知道您设置了提示变量来编辑提示
export PROMPT =这是日期%d"
如何在加载提示时每次执行命令并打印结果.
实际上有两种(主要)方法可以实现这一目标:
-
使用命令替换在提示符下运行命令
setopt提示替代PROMPT ='日期%d结果$(a_command)'
- 必须启用
-
promptsubst
,否则zsh
将不执行任何参数扩展,算术扩展或命令替换. -
此外,提示文字的引用方式应使在设置
PROMPT
时不进行扩展.因此,可以将其放在单引号中,或者,如果您要使用双引号,请在$
前面加上\
,以在必要时分别引出它们:PROMPT =日期%d结果\ $(a_command)常量$(another_command)"
设置
PROMPT
时,它将展开$(another_command)
(因此,它仅运行一次,其结果将被永久替换)和$(a_command)
每次显示提示.
-
-
使用
precmd
函数(或挂钩)和psvar
数组:自动加载-Uz add-zsh-hooka_function(){psvar [1] = $(a_command)}two_function(){psvar [2] = $(two_command)}add-zsh-hook precmd a_functionadd-zsh-hook precmd two_functionPROMPT ='日期%d结果1%v结果2%2v'
- 如果已设置,则在打印提示之前运行
precmd
函数.您还可以设置要在precmd_functions
数组中运行的函数的列表. -
add-zsh-hook
提供了一种向该数组添加函数的简便方法. 提示中的 -
%Nv
被psvar
数组的第N个元素替换.如果省略了N
(%v
),则假定为N == 1
(对于其他采用数字参数的提示令牌也是如此)
- 如果已设置,则在打印提示之前运行
乍一看,第二种方法看起来比仅使用 promptsubst
复杂得多.但这只是非常简单的替换的情况.使用 precmd
允许使用更复杂的功能,而不会由于将 $()
内的多行代码塞满而使 PROMPT
的定义不可读./p>
您还可以将两种方法结合起来,并在某些或所有情况下放弃使用 psvar
:
自动加载-Uz add-zsh-hooksetopt提示替代a_function(){a_parameter = $(a_command)}two_function(){psvar [2] = $(two_command)}add-zsh-hook precmd a_functionadd-zsh-hook precmd two_functionPROMPT ='日期%d结果$ {a_parameter}%2v'
Hi I know that you set the prompt variable to edit the prompt like this
export PROMPT="This is the date %d"
How do you execute a command and print the result everytime when prompt loads.
There are actually two (main) ways to achieve this:
Use command substitution to run a command as part of the prompt
setopt promptsubst PROMPT='Date %d Result $(a_command) '
promptsubst
has to be enabled, elsezsh
will not do any paremeter expansions, arithmetic expansions or command substitutions.Also, the prompt text needs to be quoted in such a way that the expansions are not made when setting
PROMPT
. So either put it in single quotes or, if you have/want to use double quotes, prepend$
with a\
to quote them separately where necessary:PROMPT="Date %d Result \$(a_command) Const $(another_command)"
This will expand
$(another_command)
when settingPROMPT
(so it is run only once and its result than substituted permanently) and$(a_command)
every time the prompt is shown.
Make use of the
precmd
function (or hook) and thepsvar
array:autoload -Uz add-zsh-hook a_function () { psvar[1]=$(a_command) } two_function () { psvar[2]=$(two_command) } add-zsh-hook precmd a_function add-zsh-hook precmd two_function PROMPT='Date %d Result1 %v Result2 %2v '
- If set, the
precmd
function is run just before the prompt is printed. You can also set a list of functions to run in theprecmd_functions
array. add-zsh-hook
provides an easy way to add functions to that array.%Nv
in the prompt is replaced by the N-th element of thepsvar
array. IfN
is left out (%v
)N==1
is assumed (this is also true for other prompt token that take numeric arguments)
- If set, the
On first glance the second method may look far more complicated then just using promptsubst
. But this is only the case for very simple substitutions. Using precmd
allows for using more complex functions without making the definition of PROMPT
unreadable due to cramming several lines of code inside a $( )
.
You can also combine both approaches and forego the use of psvar
in some or all cases:
autoload -Uz add-zsh-hook
setopt promptsubst
a_function () {
a_parameter=$(a_command)
}
two_function () {
psvar[2]=$(two_command)
}
add-zsh-hook precmd a_function
add-zsh-hook precmd two_function
PROMPT='Date %d Result ${a_parameter} %2v'
这篇关于Zsh提示自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!