9

我正在尝试自定义这个oh-my-zsh 主题。

我在其中找到了这段代码,它显然打印了目录名称(如果我错了,请纠正我)。

# Dir: current working directory
prompt_dir() {
  prompt_segment blue black '%~'
}

并且 prompt_segment 定义为

# Begin a segment
# Takes two arguments, background and foreground. Both can be omitted,
# rendering default background/foreground.
prompt_segment() {
  local bg fg
  [[ -n $1 ]] && bg="%K{$1}" || bg="%k"
  [[ -n $2 ]] && fg="%F{$2}" || fg="%f"
  if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
    echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
  else
    echo -n "%{$bg%}%{$fg%} "
  fi
  CURRENT_BG=$1
  [[ -n $3 ]] && echo -n $3
}

它的输出并不总是只是目录路径。如果我所在的路径也存在于 ENV 变量中,它将用该变量替换路径。

如果我在

/Users/abc/.oh-my-zsh/custom

$ZSH_CUSTOM 是

/Users/abc/.oh-my-zsh/custom

我只是进入$ZSH_CUSTOM命令提示符。

所以我的问题是,1)%~发送的内容是什么prompt_dir,2)这段代码从哪里获取当前工作目录,以及 3)我怎样才能让它总是输出真实的路径。

4

1 回答 1

16

EXPANSION OF PROMPT SEQUENCES部分man zshmisc

   %d
   /      Current  working  directory.   If an integer follows the `%', it
          specifies a number of trailing components of the current working
          directory  to show; zero means the whole path.  A negative inte‐
          ger specifies leading components, i.e. %-1d specifies the  first
          component.

   %~     As  %d  and %/, but if the current working directory has a named
          directory as its prefix, that part is replaced by a `~' followed
          by  the  name  of  the directory.  If it starts with $HOME, that
          part is replaced by a `~'.
于 2012-12-01T15:07:38.383 回答