내 zsh
현재 (왼쪽) 프롬프트는 다음과 같습니다
PROMPT="%F{106}%22<…<%3~%f%(?..%{$fg[red]%} %?%{$reset_color%})%(1j.%{$fg[cyan]%} %j%{$reset_color%}.)%# "
지정된 %22<…<
프롬프트가 22자를 초과하면 잘립니다. 프롬프트가 너무 많은 수평 공간을 차지하는 것을 원하지 않기 때문입니다.
그러나 터미널이 매우 넓은 경우 프롬프트에 22자 이상을 사용할 수 있으며 여전히 작업할 수평 공간이 충분합니다.
그래서 최대 너비가 다음과 같도록 프롬프트 형식을 지정하고 싶습니다.백분율전체 터미널 폭(예: 20%). 이를 수행할 수 있는 방법이 있습니까?
이상적으로는 터미널의 너비가 변경되면 프롬프트 너비를 다시 계산해야 합니다.
(만약의 경우: tmux
MacOS에서는 쉘이 일반적으로 내부에 있습니다)
답변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))
즉
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 %~ » '