如何配置 zsh 提示符,使其长度与终端宽度成正比

如何配置 zsh 提示符,使其长度与终端宽度成正比

zsh我当前(左侧)的提示中如下

PROMPT="%F{106}%22<…<%3~%f%(?..%{$fg[red]%} %?%{$reset_color%})%(1j.%{$fg[cyan]%} %j%{$reset_color%}.)%# "

指定%22<…<提示符长度超过 22 个字符时将被截断。这是因为我不希望提示占用太多的水平空间。

然而,当我的终端非常宽时,我可能会在提示符上花费超过 22 个字符,而且我仍然有足够的水平空间可供使用。

因此,我想格式化提示,使其最大宽度为百分比整个终端宽度(例如20%)。有没有办法做到这一点?

理想情况下,如果终端更改宽度,则应重新计算提示宽度。

(以防万一:tmux在 MacOS 上 shell 通常位于内部)

答案1

Zsh 的提示扩展行为在 中定义man zshmisc。关于设置提示长度的自定义值,它说:

   %<string<
   %>string>
   %[xstring]
      Specifies  truncation  behaviour for the remainder of the prompt
      string.   The  third,  deprecated,   form   is   equivalent   to
      `%xstringx',  i.e. x may be `<' or `>'.  The string will be dis‐
      played in place of the truncated portion  of  any  string;  note
      this does not undergo prompt expansion.

      The numeric argument, which in the third form may appear immedi‐
      ately after the `[', specifies the maximum permitted  length  of
      the various strings that can be displayed in the prompt.  In the
      first two forms, this numeric argument may be negative, in which
      case  the  truncation  length  is  determined by subtracting the
      absolute value of the numeric argument from the number of  char‐
      acter  positions  remaining on the current prompt line.  If this
      results in a zero or negative length, a length of 1 is used.  In
      other  words, a negative argument arranges that after truncation
      at least n characters remain before the right margin (left  mar‐
      gin for RPROMPT).

      The  forms  with `<' truncate at the left of the string, and the
      forms with `>' truncate at the right of the string.   For  exam‐
      ple,  if  the  current  directory  is  `/home/pike',  the prompt
      `%8<..<%/' will expand to `..e/pike'.  In this string, the  ter‐
      minating  character (`<', `>' or `]'), or in fact any character,
      may be quoted by a preceding `\'; note when using print -P, how‐
      ever, that this must be doubled as the string is also subject to
      standard  print  processing,  in  addition  to  any  backslashes
      removed  by a double quoted string:  the worst case is therefore
      `print -P "%<\\\\<<..."'.

      If the string is longer than the specified truncation length, it
      will appear in full, completely replacing the truncated string.

      The part of the prompt string to be truncated runs to the end of
      the string, or to the end of the next  enclosing  group  of  the
      `%('  construct,  or  to  the next truncation encountered at the
      same grouping level (i.e. truncations inside a  `%('  are  sepa‐
      rate), which ever comes first.  In particular, a truncation with
      argument zero (e.g., `%<<') marks the end of the  range  of  the
      string  to  be truncated while turning off truncation from there
      on. For example, the prompt  `%10<...<%~%<<%#  '  will  print  a
      truncated representation of the current directory, followed by a
      `%' or `#', followed by a space.  Without the `%<<',  those  two
      characters  would  be  included  in  the string to be truncated.
      Note that `%-0<<' is not equivalent to `%<<' but specifies  that
      the prompt is truncated at the right margin.

      Truncation  applies  only  within  each  individual  line of the
      prompt, as delimited by embedded  newlines  (if  any).   If  the
      total  length  of  any  line  of  the prompt after truncation is
      greater than the terminal width, or if the part to be  truncated
      contains embedded newlines, truncation behavior is undefined and
      may  change  in  a   future   version   of   the   shell.    Use
      `%-n(l.true-text.false-text)' to remove parts of the prompt when
      the available space is less than n.

由此你可以推断

  • PROMPT="%/ "会给你一个工作目录
  • PROMPT='%10<..<%/ '会给你一个工作目录 - 如果工作目录字符串超过 10 个字符,它将在左边缘被截断,并且在截断发生的地方,..将显示模式(以指示修剪值)

终端宽度可以通过 获得$COLUMNS,因此如果您希望提示符限制在终端宽度的 25%,请将 替换10为变量:

width=$(($COLUMNS / 4))

IE

PROMPT="%${width}<..<%/ %% "

答案2

这就是我最终写下来的。

# this variable can be changed later to change the fraction of the line 
export PROMPT_PERCENT_OF_LINE=20

# make a function, so that it can be evaluated repeatedly
function myPromptWidth() { 
  echo $(( ${COLUMNS:-80} * PROMPT_PERCENT_OF_LINE / 100 )) 
}

# for some reason you can't put a function right in PROMPT, so make an
# intermediary variable
width_part='$(myPromptWidth)'

# use ${} to evaluate the variable containing function
PROMPT="%F{106}%${width_part}<…<%3~%f%(?..%{$fg[red]%} %?%{$reset_color%})%(1j.%{$fg[cyan]%} %j%{$reset_color%}.)%# "

这将在终端调整大小时立即重新计算宽度。

答案3

# Set prompt substitution on so that ${} are evaluated. 
set -o PROMPT_SUBST

# Add a horizontal line to the prompt.
PS1=$'${(r:$COLUMNS::—:)}'

# Add UserName@HostName and pwd to the prompt.
PS1=$PS1'%n@%m %~ » '

相关内容