【问题标题】:zsh declare PROMPT using multiple lineszsh 使用多行声明 PROMPT
【发布时间】:2020-10-31 16:14:01
【问题描述】:

我想使用多行和 cmets 声明我的 ZSH 提示符,例如:

PROMPT="
    %n       # username
    @
    %m       # hostname
    \        # space
    %~       # directory
    $
    \        # space
"

(例如 perl 正则表达式的“忽略空格模式”)

我可以发誓我曾经做过这样的事情,但再也找不到那些旧文件了。我搜索了“zsh declare prompt across multiple lines”的变体,但还没有找到。

我知道我可以使用 \ 来续行,但我们最终会使用换行符和空格。

编辑:也许我记错了 cmets - 这是一个 example without comments

【问题讨论】:

  • 由于您的代码中的# 在字符串中,因此它们不会引入 cmets,而是字符串的文字部分。 zsh 中没有像 Perl Regexp 中那样 ignore whitespace 的东西。

标签: variables zsh prompt


【解决方案1】:

不完全是您要查找的内容,但您无需在单个作业中定义 PROMPT

PROMPT="%n"    # username
PROMPT+="@%m"  # @hostname
PROMPT+=" %~"  # directory
PROMPT+="$ "

可能更接近您想要的是连接数组元素的能力:

prompt_components=(
   %n   # username
   " "  # space
   %m   # hostname
   " "  # space
   "%~"  # directory
   "$"
)
PROMPT=${(j::)prompt_components}

或者,您可以让j 标志添加空格分隔符,而不是将它们放入数组中:

# This is slightly different from the above, as it will put a space
# between the director and the $ (which IMO would look better).
# I leave it as an exercise to figure out how to prevent that.
prompt_components=(
 "%n@%m"  # username@hostname
 "$~"  # directory
 "$" 
)

PROMPT=${(j: :)prompt_components}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    相关资源
    最近更新 更多