Zsh提示自定义 [英] Zsh Prompt Customization

查看:119
本文介绍了Zsh提示自定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您设置了提示变量来编辑提示

  export PROMPT =这是日期%d" 

如何在加载提示时每次执行命令并打印结果.

解决方案

实际上有两种(主要)方法可以实现这一目标:

  1. 使用命令替换在提示符下运行命令

      setopt提示替代PROMPT ='日期%d结果$(a_command)' 

      必须启用
    • promptsubst ,否则 zsh 将不执行任何参数扩展,算术扩展或命令替换.
    • 此外,提示文字的引用方式应使在设置 PROMPT 时不进行扩展.因此,可以将其放在单引号中,或者,如果您要使用双引号,请在 $ 前面加上 \ ,以在必要时分别引出它们:

        PROMPT =日期%d结果\ $(a_command)常量$(another_command)" 

      设置 PROMPT 时,它将展开 $(another_command)(因此,它仅运行一次,其结果将被永久替换)和 $(a_command)每次显示提示.

  2. 使用 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:

  1. 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, else zsh 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 setting PROMPT (so it is run only once and its result than substituted permanently) and $(a_command) every time the prompt is shown.

  2. Make use of the precmd function (or hook) and the psvar 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 the precmd_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 the psvar array. If N is left out (%v) N==1 is assumed (this is also true for other prompt token that take numeric arguments)

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆