【问题标题】:How do I get ZSH to display the current directory in the Terminal frame?如何让 ZSH 在终端框架中显示当前目录?
【发布时间】:2012-06-17 00:17:09
【问题描述】:

我正在逐渐从 Bash 切换到 ZSH,并尝试通过复制功能来学习。不过好像找不到这个。

我应该放什么,我应该放哪里?这甚至可能吗?谢谢。

【问题讨论】:

  • 它可能是由bash下的shell提示设置的——你从bash下的echo $PS1得到什么输出?
  • AFAIK,终端只是这个。我的 $PS1 只是设置为无穷大符号 ∞。
  • zsh 提供了一个chpwd_functions 数组,它比使用基于PS1 或基于precmd 的转义要好得多。我希望PS1Terminal.app 转义序列来设置窗口标题。也许它在PROMPT_COMMAND
  • 我真的不知道。这对我来说都是全新的。我正在查看 github.com/holman/dotfiles/tree/master/zsh 并想知道它是否 prompt.zsh 可能是需要发生的地方。关于上一个链接,我也错了。误报。

标签: macos terminal zsh


【解决方案1】:

我已经定义了这个函数。需要一些 Python 知识并且有点冗长,但我知道那里发生了什么。

没有谷歌搜索我不知道为什么 sarnold 的 printf "\e]0;$@\a" 会做它应该做的事情。

function current_directory()
{
    echo $(python3 -c "from pathlib import Path; print(Path('.').absolute().name)")
}

【讨论】:

    【解决方案2】:

    试试这个:

    settitle() { printf "\e]0;$@\a" }
    dir_in_title() { settitle $PWD }
    chpwd_functions=(dir_in_title)
    

    现在,您的cd 命令将运行dir_in_title 函数,该函数将打印一个转义序列,要求Terminal.app 更新标题。 (奇怪的是,至少使用了在urxvt 中也可以使用的转义序列。这些必须比我预期的更标准化。)

    如果您喜欢这种效果,您需要将这些行添加到您的 ~/.zshrc 中,以便它在未来的终端上运行。

    我抓住了正确的转义序列from Chris Page on superuser 和函数风格from my answer to similar but different questionChris Page gave his own answer on that question with details on OS X 10.7 that are drastically different。当您升级时,您可能希望使用他的机制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多