Shell ZSH挂起打开的应用程序选项卡自动完成
我经常使用命令Shell ZSH挂起打开的应用程序选项卡自动完成,shell,command-line,terminal,zsh,zsh-completion,Shell,Command Line,Terminal,Zsh,Zsh Completion,我经常使用命令open-a在Mac上打开应用程序。最近我还一直在使用制表符自动完成来完成我的命令。例如,我在写了open-a或open-a Safa之后尝试这样做,但当我点击tab时,zsh挂起,我无法在窗口中键入任何其他内容。这也没有提供应有的自动完成选项。我在Terminal和iTerm2中都试过了,但问题仍然存在 我使用的是zsh 5.7.1。我的电脑上也安装了Oh My Zsh 为了以防万一,这是我的.zshrc文件: # If you come from bash you might
open-a
在Mac上打开应用程序。最近我还一直在使用制表符自动完成来完成我的命令。例如,我在写了open-a
或open-a Safa
之后尝试这样做,但当我点击tab时,zsh挂起,我无法在窗口中键入任何其他内容。这也没有提供应有的自动完成选项。我在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错误!问题在于苹果自己的
open
compdef完成脚本
在过去,open-a
完成速度很快,因为autocomplete脚本使用Spotlight索引进行超快速的应用程序获取。您可以在此处找到相关脚本:/usr/share/zsh/5.7.1/functions/\u retrieve\u mac\u apps
(参考同一目录中的\u open
脚本)
但是,要检查Spotlight搜索索引是否存在,它将查看文件/.Spotlight-V100
是否存在。在某些macOS更新中,用户文件的聚光灯索引移动到此处:/System/Volumes/Data/.Spotlight-V100
,但自动完成脚本未更新!当它认为不存在聚光灯索引时,它会退回到一个非常缓慢的手动应用程序列表
具体而言,错误在于此检查:
...
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
...
可悲的是,由于这个bug是macOS本身的一部分,所以该文件受系统完整性保护,因此修复起来非常烦人。您可以复制它,修复错误,并用您的固定版本替换open的compdef。希望苹果最终能在大苏尔解决这个问题
当我点击tab时,zsh挂起
-您等了多长时间?你试过ctrl+c吗?@KamilCuk它会无限期地运行而不需要干预(在关闭终端并重试之前,我最多离开它15分钟)。Control+c显示消息“Killed by signal in_mac_apps_old_retrieve after 2s”,然后继续单击enter,它只表示找不到具有缩短名称的应用程序(这不是全名,因为我正在寻找自动完成的应用程序)@NoahWilder:我想这只是在open
的上下文中,也就是说,当你做一个简单的cat
时,你看不到这种效果?@1934428这只发生在open-a
的上下文中。open
和cat
都能按预期执行。这并不能回答您明确的问题,但应该解决您的使用案例——VSCode附带了命令行工具,您可以从命令对话框安装,该对话框将code
添加为您可以从命令行使用的命令。要达到上述目的,只需键入code.
即可在VSC中打开当前目录!