Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tab completion not working #16

Closed
mihai-dinu opened this issue Feb 26, 2020 · 15 comments
Closed

Tab completion not working #16

mihai-dinu opened this issue Feb 26, 2020 · 15 comments
Assignees

Comments

@mihai-dinu
Copy link

Hello! First off, thank you so much for the plugin, it's awesome!

Secondly, I'm running a bare bones zsh with just a .zshrc file that I've created and for some reason tab completion does not work with z.

This is the contents of the .zshrc

bindkey -e
# End of lines configured by zsh-newuser-install

# The following lines were added by compinstall
zstyle :compinstall filename '${HOME}/.zshrc'

autoload -Uz compinit
compinit
# End of lines added by compinstall

# Allow comments even in interactive shells.
setopt interactive_comments
# Add command to history file immediately after hitting Enter
setopt inc_append_history
# Share history between terminals
setopt share_history

# Zstyle stuff
zstyle ':completion:*' menu select

# Aliases
alias ls='ls -G -F'
alias ll='ls -G -F -lh'
alias zshconfig="vim ~/.zshrc"
alias zr="source ~/.zshrc"
alias gs="git status"
alias gco="git checkout"
alias gcu="git commit --amend --no-edit"
alias gd="git diff"
alias c="code"
alias cat="bat"
alias python="python3"
alias e="open -a /Applications/Emacs.app $@"
alias em="/usr/local/bin/emacsclient -n"

## Functions
mkcdir() {
  mkdir -p -- "$1" &&
    cd -P -- "$1"
}

dclean() {
  docker stop $(docker ps -aq)
  docker rm -v $(docker ps -aq)
}

# Exports
# Remove duplicates from $PATH
typeset -U PATH path
export PATH=$PATH:$HOME/.toolbox/bin:$HOME/Library/Python/3.7/bin
export EDITOR="/Applications/Emacs.app/Contents/MacOS/Emacs"
export JAVA_HOME="$(/usr/libexec/java_home)"
export ZSH_PLUGIN_DIR="$HOME/.zsh_plugins"

# Plugins
# Z
source $ZSH_PLUGIN_DIR/zsh-z/zsh-z.plugin.zsh

Contents of z -l:

 ~ 👉 z -l
3.85       /Users/dimiha/scripts
7.78       /private/tmp
....

When I type z tm<TAB>, i don't get the path expanded to /private/tmp.

Am i doing anything wrong?

@agkozak agkozak self-assigned this Feb 26, 2020
@agkozak
Copy link
Owner

agkozak commented Feb 26, 2020

Try putting

autoload -Uz compinit
compinit

after

source $ZSH_PLUGIN_DIR/zsh-z/zsh-z.plugin.zsh

Let me know if that helps!

@mihai-dinu
Copy link
Author

That was it. Did not know that the order mattered.

Thank you for the help and again for the awesome plugin!

@agkozak
Copy link
Owner

agkozak commented Feb 26, 2020

No problem. Enjoy!

@agkozak agkozak closed this as completed Feb 26, 2020
@borekb
Copy link

borekb commented Apr 1, 2021

@agkozak I'm a total zsh noob so could you please explain why autoload -U compinit && compinit has to be run explicitly? Both Oh My Zsh (which I use) or Znap (which I also use) set this automatically, I think. zsh-z is the only script after which I have to run compinit again, why is that?

@agkozak
Copy link
Owner

agkozak commented Apr 1, 2021

Great question!

You should never run compinit more than once. It's the slowest part of zsh.

My documentation says

For tab completion to work, you will want to have loaded compinit. The frameworks handle this themselves.

As soon as I've had a bit more coffee, I'll try rewriting that to say something like, "Most frameworks load compinit for you, but if you aren't using one, you should do it, etc. etc." I might even say, "Do check to make sure you're not loading compinit more than once, as it can be slow."

But @borekb, are you having a more specific problem where you seem to need to load it twice? If so, feel free to share your .zshrc and I'll try to figure out what's going on. Usually it's just a question of putting everything in the right order.

@borekb
Copy link

borekb commented Apr 1, 2021

Yes, I think my problem is more specific – this is my .zshrc (shortened but the order is kept):

# "Automatic compinit": https://github.com/marlonrichert/zsh-snap#automatic-compinit-and-bashcompinit
source ~/.znap/zsh-snap/znap.zsh

# Should probably call compinit as well? https://github.com/ohmyzsh/ohmyzsh/blob/71cc861/oh-my-zsh.sh#L21
znap source ohmyzsh/ohmyzsh

# Finally, zhs-z:
znap source agkozak/zsh-z
autoload -U compinit && compinit # I must call this to see completions

@agkozak
Copy link
Owner

agkozak commented Apr 1, 2021

There's a lot going on behind the scenes in your example. compinit has to be run after agkozak/zsh-z is sourced, but it looks as if Oh-My-ZSH runs compinit itself. Without reading all the code, my guess is that znap can tell compinit has been run and does not run it again.

You can fix this problem by sourcing agkozak/zsh-z first, and ohmyzsh/ohmyzsh later. Then you don't have to call compinit yourself.

I'll do some experimenting, but I'll probably just update the README.md to cover your example (I don't think znap existed when I got started, but I could be wrong).

@borekb
Copy link

borekb commented Apr 1, 2021

One more thing I wonder about: what makes zsh-z special that it requires the compinit call?

Oh I just realized something that I didn't before. I have other completions like zsh-users/zsh-completions or one for gcloud utility and I thought they "don't require compinit". But they do, it just happened that this is the order of my .zshrc:

source ~/.znap/zsh-snap/znap.zsh
znap source ohmyzsh/ohmyzsh
znap source zsh-users/zsh-completions
source '/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/completion.zsh.inc'
znap source agkozak/zsh-z

agkozak/zsh-z is one of the last things, and I use z quite a lot so I noticed the problem there first. And since I added compinit at the end of the file, the other completions work as well.

I just tried removing compinit and also e.g. zsh-completions are broken. Of course 🤦.

Also found this example of znap compdef:

https://github.com/marlonrichert/zsh-snap/blob/9786914fecf42948597abfe99436e26a3fc7997b/.zshrc#L62-L65

Not yet sure if this relates to my issue or not...

@agkozak
Copy link
Owner

agkozak commented Apr 2, 2021

If you type

which compinit

you’ll see that znap does something interesting: it redefines compinit to make it do nothing. znap runs the real compinit routine when it thinks it should. I suspect it’s trying to avoid the usual situation where people run it too many times and slow down their zsh startup.

@borekb
Copy link

borekb commented Apr 2, 2021

Interesting.. Also, Oh My Zsh seems to be somehow significant, I've posted a couple of examples here: marlonrichert/zsh-snap#61 (reply in thread).

@agkozak
Copy link
Owner

agkozak commented Apr 2, 2021

Thanks for showing me that thread! I think you're right about Oh-My-ZSH.

@mihai-dinu
Copy link
Author

Hi! Me again 😬

I switched laptops (still a Mac, but this time it's M1 and with Big Sur). I ended up having the same problem again. Went through the initial fix, but that doesn't seem to do it for me anymore.

Here's my .zshrc - i stripped it down to just this:

[[ -d  $HOME/.zsh/completion ]] || mkdir -p $HOME/.zsh/completion
export ZSH_PLUGIN_DIR="$HOME/.zsh/plugins"

# Z
([[ -f $ZSH_PLUGIN_DIR/zsh-z/zsh-z.plugin.zsh ]] \
  || (mkdir -p $ZSH_PLUGIN_DIR/zsh-z \
  && wget https://raw.githubusercontent.com/agkozak/zsh-z/master/zsh-z.plugin.zsh -O $ZSH_PLUGIN_DIR/zsh-z/zsh-z.plugin.zsh)) \
&& source $ZSH_PLUGIN_DIR/zsh-z/zsh-z.plugin.zsh 

autoload -Uz compinit
compinit

And the output from setopt:

combiningchars
interactive
login
monitor
shinstdin
zle

Again, I can see the database being populated with directories, but I don't get tab completion when typing z <some_dir_substring>.

Do you have any ideas on what I can try to debug this further?

Thanks once again!

@agkozak
Copy link
Owner

agkozak commented Oct 13, 2021

Yes, I can see what's happening. You're downloading and sourcing zsh-z.plugin.zsh, and it tries to add another file, _zshz, to the FPATH, but _zshz isn't there. Make sure to download it, too, and keep it in the same directory as zsh-z.plugin.zsh. The easiest way, of course, is just to get the whole repository using Git:

git clone https://github.com/agkozak/zsh-z

@mihai-dinu
Copy link
Author

mihai-dinu commented Oct 14, 2021

As always, you are right! Thank you very much!

@agkozak
Copy link
Owner

agkozak commented Oct 14, 2021

@mihai-dinu You're very welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants