zsh缩短当前路径的长度

10
每当我进入一个更深层次的目录时,zsh会在提示符中显示完整路径。
而我更喜欢显示: x@y:~/path/somewhere 而不是 x@y:~/i/am/a/really/really/really/really/long/path/somewhere 如何实现这个功能?
我正在使用OSX Yosemite 10.10.4上的iTerm和zsh。
编辑:
这是我的bashrc文件:
  1 # System-wide .bashrc file for interactive bash(1) shells.
  2 if [ -z "$PS1" ]; then
  3    return
  4 fi
  5
  6 PS1='\h:\W \u\$ '
  7 # Make bash check its window size after a process completes
  8 shopt -s checkwinsize
  9 # Tell the terminal about the working directory at each prompt.
  10 if [ "$TERM_PROGRAM" == "Apple_Terminal" ] && [ -z "$INSIDE_EMACS" ]; then
  11     update_terminal_cwd() {
  12         # Identify the directory using a "file:" scheme URL,
  13         # including the host name to disambiguate local vs.
  14         # remote connections. Percent-escape spaces.
  15         local SEARCH=' '
  16         local REPLACE='%20'
  17         local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
  18         printf '\e]7;%s\a' "$PWD_URL"
  19     }
  20     PROMPT_COMMAND="update_terminal_cwd; $PROMPT_COMMAND"
  21 fi

1
相关:在bash提示符上仅显示当前目录名称(而不是完整路径)。在被接受的答案中,您可以找到一些关于调整PS1的好信息。 - fedorqui
考虑查看答案中提供的链接。此外,您当前的PS1与此处指示的定义不匹配。我建议在您的配置文件中使用grep查找更多关于PS1的定义。请尝试运行命令:grep PS1 ~/.* - fedorqui
很奇怪。如果你源化你的bashrc文件会怎样呢?可能是 source ~/.bashrc。如果它被定义得很好,提示符应该会改变。 - fedorqui
1
你的文件可能没有被读取或格式不正确,因此未设置PS1。在https://wiki.gentoo.org/wiki/Zsh/Guide中,他们解释了如何设置PS1。我建议按照他们提出的测试(在新标签页中!)`export PS1="[Test Prompt] > "`。 - fedorqui
Bash提示转义序列与zsh不兼容。 - baf
显示剩余5条评论
1个回答

22
为了指定要显示的路径尾部组件数量,请将整数插入到提示符中相应的转义序列中。在你的情况下,%2~ 将起到作用。摘自zshmisc(1):
%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 integer specifies leading  components, i.e. %-1d 
       specifies the first component.

%~     As  %d  and  %/, but if the current working directory starts
       with $HOME, that part is replaced by a `~'. Furthermore, if 
       it has a named directory as its prefix, that part is replaced 
       by a `~' followed by the name of the directory, but only if 
       the result is shorter than the full path; see Dynamic and 
       Static named directories in zshexpn(1).

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接