ZSH在打开应用程序标签自动完成时卡住

6
我经常使用命令open -a <应用程序名称>在我的Mac上打开应用程序。最近我也开始更多地使用Tab自动完成来完成我的命令。例如,当我写完open -aopen -a Safa后,我尝试使用Tab自动补全,但是zsh卡住了,我无法在窗口中输入任何其他内容。这也没有提供应有的自动补全选项。我在终端和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的自动补全功能,就像在其他地方一样?涉及到it技术。请注意保留html标记格式。

当我按下Tab键时,zsh卡住了。你等了多久?你尝试过Ctrl+C吗? - KamilCuk
@KamilCuk它在没有干预的情况下无限期地继续(我为其留下的最长时间是15分钟,然后关闭终端并重试)。按Control + c会显示消息“在_mac_apps_old_retrieve中被信号杀死2秒后” ,然后按Enter键,它只是说无法找到缩写名称的应用程序(这不是完整名称,因为我正在寻找自动完成的名称)。 - Noah Wilder
@NoahWilder:我猜这只是在“打开”上下文中,也就是当你执行简单的“cat <TAB>”时,你看不到这种效果? - user1934428
@1934428 这种情况只会在 open -a <TAB> 的上下文中发生。open <TAB>cat <TAB> 的表现都符合预期。 - Noah Wilder
这并不直接回答你的问题,但应该能解决你的使用情况 -- VSCode自带命令行工具,你可以从命令对话框中安装它们,这会将code添加为你可以从命令行使用的命令。要实现你上述的目的,只需在命令行中键入code .即可在VSC中打开当前目录! - ibiwan
1个回答

5

实际上这是一个macOS的bug!问题出现在苹果自己的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。希望苹果公司能在Big Sur中最终解决这个问题!

编辑:这里有一个修复了这个问题的gist,并附有一些安装说明:https://gist.github.com/varenc/d2c09c3ad5939774001c32fe5f7df41f

另外,作为参考,苹果公司在Big Sur或Monterey中并没有解决这个问题。


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