这是我对zsh的/ etc / bashrc的改编。我已包括所有需要它的URL字符的百分比编码,如果您希望它与所有有效的文件名和目录名一起使用,则这很重要。
这将注册一个precmd
挂钩,该挂钩允许在其他脚本和配置文件中注册多个功能。
2019年3月更新:设置LC_ALL
为空,因此不会覆盖LC_CTYPE
。用于precmd
在每个提示符下更新工作目录,而不是在每次chpwd
更改时都进行更新-命令管道可能会临时更改它,并且终端不应显示这些目录。同样,如果在上一个命令中更改了终端状态,则每个提示都将更新终端状态可能会有帮助。使用printf -v
明确写入该变量,而不是使用子shell语法。
# Tell the terminal about the working directory whenever it changes.
if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
update_terminal_cwd() {
# Identify the directory using a "file:" scheme URL, including
# the host name to disambiguate local vs. remote paths.
# Percent-encode the pathname.
local url_path=''
{
# Use LC_CTYPE=C to process text byte-by-byte. Ensure that
# LC_ALL isn't set, so it doesn't interfere.
local i ch hexch LC_CTYPE=C LC_ALL=
for ((i = 1; i <= ${#PWD}; ++i)); do
ch="$PWD[i]"
if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
url_path+="$ch"
else
printf -v hexch "%02X" "'$ch"
url_path+="%$hexch"
fi
done
}
printf '\e]7;%s\a' "file://$HOST$url_path"
}
# Register the function so it is called at each prompt.
autoload add-zsh-hook
add-zsh-hook precmd update_terminal_cwd
fi