【问题标题】:PS1 config in zsh causes previous line to be removed after each prompt redrawzsh 中的 PS1 配置会导致在每次提示重绘后删除前一行
【发布时间】:2016-10-04 14:16:07
【问题描述】:

我的.zsh 配置有以下几行:

function zle-line-init zle-keymap-select {
    VIM_PROMPT="${${KEYMAP/vicmd/ [NORMAL]}/(main|viins)/}"
    PS1=$'\e[0;36m%n\e[m at \e[0;33m%m\e[m in \e[0;32m%~\e[m\e[0;34m$(vcs_info_wrapper)\e[m\e[1;33m$VIM_PROMPT\e[m\n$ '
    zle reset-prompt
}

如果终端窗口太窄,每次重绘提示时都会删除前一行。例如,假设我有以下提示:

first line
some text here
another text here
sergiyb at wing in ~ [master *]
$ 

然后我按 Escape 并得到

first line
some text here
sergiyb at wing in ~ [master *] [NORMAL]
$ 

现在如果我按 i,那么我会得到这个

first line
sergiyb at wing in ~ [master *]
$ 

同样,如果我输入命令ls,它会列出所有文件,但会删除最后一行:

sergiyb at wing in ~ [master *]
$ ls test_dir_with_files_1_to_10
file1   file2  file4  file6  file8

但是,如果终端窗口足够宽,则不会发生这种情况,例如

sergiyb at wing in ~ [master *]
$ ls test_dir_with_files_1_to_10
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9

如果我尝试缩短我的 PS1 线,例如通过删除用户名,它会停止发生,直到我进一步减小终端的宽度。

知道是什么原因造成的和/或如何解决?

【问题讨论】:

    标签: ubuntu-14.04 zsh


    【解决方案1】:

    zsh 无法准确确定提示的大小,因为它不知道终端不会将 ANSI 转义码显示为可打印字符。请改用zsh 自己的格式转义。

    PS1=$'%F{cyan}%n%f at %F{yellow}%m%f in %F{green}%~%F{blue}$(vcs_info_wrapper)%B%F{yellow}$VIM_PROMPT%f%b\n$ '
    

    【讨论】:

    • 我可以用这些得到亮黄色和深黄色吗?在 ANSI 转义码中,这些代码是 \e[0;33m\e[1;33m
    • %B/%b 对应该为第二个黄色单词启用粗体/明亮。
    猜你喜欢
    • 2020-11-07
    • 2023-03-08
    • 2018-06-07
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多