ZSH 在打开应用程序选项卡自动完成时挂起

ZSH Hangs on Open Application Tab Autocompletion

我经常使用命令 open -a <application-name> 在 Mac 上打开应用程序。我最近也一直在使用制表符自动完成来完成我的命令。我尝试在写入后执行此操作,例如,open -aopen -a Safa,但是当我点击 tab 时,zsh 挂起并且我无法在 window 中键入任何其他内容。这也没有提供应有的自动完成选项。我在 Terminal 和 iTerm2 中都试过了,问题仍然存在。

我正在使用 zsh 5.7.1。我的电脑上也安装了 Oh My Zsh。

为了以防万一,这是我的 .zshrc 文件:

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH="/Users/noahwilder/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="spaceship"
#"robbyrussell"
#"agnoster"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in ~/.oh-my-zsh/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"

# Uncomment the following line to automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"

# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS=true

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in ~/.oh-my-zsh/plugins/*
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
  git
  zsh-autosuggestions
  zsh-syntax-highlighting
  Z
)

source $ZSH/oh-my-zsh.sh

# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"

# Other setup
autoload -U compinit && compinit
zmodload -i zsh/complist
eval $(thefuck --alias)
. /usr/local/etc/profile.d/z.sh

#prompt_context(){}

# Spaceship theme:
# 
ZSH_THEME="spaceship"
autoload -U promptinit; promptinit;
prompt spaceship
SPACESHIP_BATTERY_SHOW=false


# Use Pure theme:
#
# autoload -U promptinit; promptinit
# prompt pure

# This speeds up pasting w/ autosuggest
# https://github.com/zsh-users/zsh-autosuggestions/issues/238
pasteinit() {
  OLD_SELF_INSERT=${${(s.:.)widgets[self-insert]}[2,3]}
  zle -N self-insert url-quote-magic # I wonder if you'd need `.url-quote-magic`?
}

pastefinish() {
  zle -N self-insert $OLD_SELF_INSERT
}
zstyle :bracketed-paste-magic paste-init pasteinit
zstyle :bracketed-paste-magic paste-finish pastefinish

如何解决此问题并启用自动完成以使用 zsh 打开应用程序,就像在 zsh 的其他地方一样可用?

这实际上是一个 macOS 错误!问题出在 Apple 自己的 open compdef 完成脚本中。

过去 open -a 完成速度很快,因为自动完成脚本使用 Spotlight 索引来实现超快的应用程序抓取。您可以在此处找到相关脚本:/usr/share/zsh/5.7.1/functions/_retrieve_mac_apps(引用自同一目录中的 _open 脚本)

但是,要检查 Spotlight 搜索索引是否存在,它会查看文件 /.Spotlight-V100 是否存在。在某些 macOS 更新中,用户文件的 Spotlight 索引已移至此处:/System/Volumes/Data/.Spotlight-V100 但自动完成脚本未更新!当它认为不存在 Spotlight 索引时,它会回退到非常缓慢的手动应用程序列表。

具体错误是这个检查:

...
      if [[ -d /.Spotlight-V100 ]]; then
        # / is indexed to use Spotlight
         retrieve=_mac_apps_spotlight_retrieve
       else
         # Fall back to the old way
         retrieve=_mac_apps_old_retrieve
       fi
       zstyle ":completion:*:*:${service}:commands" search-method $retrieve

缓慢的实施然后完成这个神秘的 shell-fu。在试验时,它似乎永远挂在我身上。

 _mac_apps_old_retrieve () {
   # Get directories which may contain applications
   typeset -aU app_dir
   if [[ -z "$app_dir" ]] && \
     ! zstyle -a ":completion:${curcontext}:commands" application-dir app_dir
   then
     typeset -a app_dir_stop_pattern
     app_dir_stop_pattern=( "*.app" "contents#" "*data" "*plugins#" "*plug?ins#" "fonts#" "document[[:alpha:]]#" "*help" "resources#" "images#" "*configurations#" )
     typeset app_dir_pattern
     app_dir_pattern="(^(#i)(${(j/|/)app_dir_stop_pattern}))"
     app_dir=( ${^app_dir_root}/(${~app_dir_pattern}/)#(N) )
   fi
...

遗憾的是,由于此错误是 macOS 本身的一部分,因此该文件受系统完整性保护保护,因此修复起来非常烦人。不过,您可以复制它,修复错误,并将 open 的 compdef 替换为您的固定版本。希望 Apple 最终能在大苏尔解决这个问题!

编辑:这是解决问题的要点,并提供了一些安装说明:https://gist.github.com/varenc/d2c09c3ad5939774001c32fe5f7df41f

另供参考,Apple 并未在 Big Sur 或 Monterey 修复此问题。