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

[Feature Request] Tab complete stored locations #1

Closed
ghost opened this issue Dec 15, 2018 · 18 comments
Closed

[Feature Request] Tab complete stored locations #1

ghost opened this issue Dec 15, 2018 · 18 comments
Assignees

Comments

@ghost
Copy link

ghost commented Dec 15, 2018

I would like to be able to hit z [tab] and get a list of paths I been to. Currently it just displays directories/files in the current path. This would be in line with z and fasd.

@agkozak
Copy link
Owner

agkozak commented Dec 15, 2018

@gregf - The behavior you describe is exactly how it is supposed to work - you can see it in the demo starting around 0:32.

My suspicion is that if tab completion is working for you with rupa/z and not with zsh-z, it's because I'm using the "new" compsys completion system and rupa/z uses the old compctl one. There could be some conflict with your settings, and it would be helpful for me to identify that conflict.

Do you happen to have any zstyle declarations in your .zshrc? That would be the most likely source of trouble.

Perhaps you could go to the directory where you've got zsh-z and run

zsh -f
source zsh-z.plugin.zsh

That will start zsh without your default settings and then load zsh-z. Try doing that and see if you get proper z-style results. We'll work from there.

Thanks so much for writing in. It is very useful for me to know what problems people run into.

@ghost
Copy link
Author

ghost commented Dec 15, 2018

Running zsh -f and sourcing zsh-z.plugin.zsh fixed the issue. This is my completion.zsh i source in my zshrc file. Could you tell me what the issue is because I have no idea? Thank you so much.

## enable caching
zstyle ':completion::complete:*' use-cache on
zstyle ':completion::complete:*' cache-path ~/.zcache

## enable case-insensitive completion
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'

## various tweaks
zstyle '*' single-ignored show

zstyle ':completion:*' completer _complete _match _approximate
zstyle ':completion:*:match:*' original only
zstyle ':completion:*:approximate:*' max-errors 1 numeric

zstyle -e ':completion:*:approximate:*' max-errors 'reply=($((($#PREFIX+$#SUFFIX)/3))numeric)'

## history
zstyle ':completion:*:history-words' stop yes
zstyle ':completion:*:history-words' remove-all-dups yes
zstyle ':completion:*:history-words' list false
zstyle ':completion:*:history-words' menu yes

## directories
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*:*:cd:*' tag-order local-directories directory-stack path-directories
zstyle ':completion:*:*:cd:*:directory-stack' menu yes select
	zstyle ':completion:*:-tilde-:*' group-order 'named-directories' 'path-directories' 'users' 'expand'
	zstyle ':completion:*' squeeze-slashes true

## ignore completions for commands that we dont have
	zstyle ':completion:*:functions' ignored-patterns '(_*|pre(cmd|exec))'

## array completion
zstyle ':completion:*:*:-subscript-:*' tag-order indexes parameters

## group matches and describe
zstyle ':completion:*:*:*:*:*' menu select
	zstyle ':completion:*:matches' group 'yes'
	zstyle ':completion:*:options' description 'yes'
	zstyle ':completion:*:options' auto-description '%d'
	zstyle ':completion:*:corrections' format ' %F{green}-- %d (errors: %e) --%f'
	zstyle ':completion:*:descriptions' format ' %F{yellow}-- %d --%f'
	zstyle ':completion:*:messages' format ' %F{purple} -- %d --%f'
	zstyle ':completion:*:warnings' format ' %F{red}-- no matches found --%f'
	zstyle ':completion:*:default' list-prompt '%S%M matches%s'
	zstyle ':completion:*' format ' %F{yellow}-- %d --%f'
	zstyle ':completion:*' group-name ''
	zstyle ':completion:*' verbose yes

## pretty menu's
zstyle ':completion:*' menu select=1
	zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
	setopt auto_menu    # show menu on 2nd <tab>
	setopt list_rows_first  # use row list if possible

## prevent re-suggestion
zstyle ':completion:*:(scp|rm|kill|diff):*' ignore-line yes
#zstyle ':completion:*:rm:*' file-patterns '*:all-files'

## enable killall menu (linux)
zstyle ':completion:*:processes-names' command 'ps -u $(whoami) -o comm='
zstyle ':completion:*:processes-names' menu yes select
	zstyle ':completion:*:processes-names' force-list always

zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;32'
zstyle ':completion:*:*:kill:*' menu yes select
zstyle ':completion:*:*:kill:*' force-list always
zstyle ':completion:*:*:kill:*' insert-ids single

## man pages
zstyle ':completion:*:manuals' separate-sections true
zstyle ':completion:*:manuals.(^1*)' insert-sections true

## enable make completion
compile=(all clean compile disclean install remove uninstall buildworld kernel)
compctl -k compile make

## user completion cleanup
zstyle ':completion:*:*:*:users' ignored-patterns \
	adm amanda apache avahi backup beaglidx bin cacti canna clamav daemon \
	dladm dbus distcache dovecot fax ftp games gdm gkrellmd gopher gnats \
	hacluster haldaemon halt hsqldb ident junkbust ldap lp irc list libuuid \
	listen mdns mail mailman mailnull mldonkey mysql man messagebus \
	netadm netcfg nagios noaccess nobody4 nuucp \
	named netdump news nfsnobody nobody nscd ntp nut nx openvpn openldap \
	operator pcap pkg5srv postfix postgres proxy privoxy pulse pvm quagga radvd \
	rpc rpcuser rpm shutdown statd squid sshd sync sys syslog uucp vcsa \
	smmsp svctag upnp unknown webservd www-data xfs xvm zfssnap '_*'

## hostname completion
if [ ! -f ~/.ssh/config ]; then
	[ -f ~/.ssh/known_hosts ] && rm ~/.ssh/known_hosts
	mkdir -p ~/.ssh
	echo "HashKnownHosts no" >>! ~/.ssh/config
	chmod 0600 ~/.ssh/config
fi
zstyle -e ':completion:*:hosts' hosts 'reply=(
${=${=${=${${(f)"$(cat {/etc/ssh_,~/.ssh/known_}hosts(|2)(N) 2>/dev/null)"}%%[#| ]*}//\]:[0-9]*/ }//,/ }//\[/ }
${=${(f)"$(cat /etc/hosts(|)(N) <<(ypcat hosts 2>/dev/null))"}%%\#*}
	${=${${${${(@M)${(f)"$(cat ~/.ssh/config 2>/dev/null)"}:#Host *}#Host }:#*\**}:#*\?*}}
		)'

## ssh/scp/rsync
zstyle ':completion:*:(scp|rsync):*' tag-order 'hosts:-host:host hosts:-domain:domain hosts:-ipaddr:ip\ address *'
zstyle ':completion:*:(scp|rsync):*' group-order users files all-files hosts-domain hosts-host hosts-ipaddr
zstyle ':completion:*:ssh:*' tag-order 'hosts:-host:host hosts:-domain:domain hosts:-ipaddr:ip\ address *'
zstyle ':completion:*:ssh:*' group-order users hosts-domain hosts-host users hosts-ipaddr
zstyle ':completion:*:(ssh|scp|rsync):*:hosts-host' ignored-patterns '*(.|:)*' loopback ip6-loopback localhost ip6-localhost ip6-allnodes ip6-allrouters ip6-localnet ip6-mcastprefix broadcasthost
zstyle ':completion:*:(ssh|scp|rsync):*:hosts-domain' ignored-patterns '<->.<->.<->.<->' '^[-[:alnum:]]##(.[-[:alnum:]]##)##' '*@*'
zstyle ':completion:*:(ssh|scp|rsync):*:hosts-ipaddr' ignored-patterns '^(<->.<->.<->.<->|(|::)([[:xdigit:].]##:(#c,2))##(|%*))' '127.0.0.<->' '255.255.255.255' '::1' 'fe80::*' 'fe00::*' 'ff00::*' 'ff02::*'
# }}}

@ghost
Copy link
Author

ghost commented Dec 15, 2018

Maybe something wrong in here too?

#!/bin/zsh -f

################################################################################
# Get keys working
#
# For this to work you have to first run 'zsh /usr/share/zsh/4.3.4/functions/Misc/zkbd'.
# The location of this script on your system may vary.
# Also note you must change the 'source' line below to match your zkbd config.
#################################################################################

autoload -Uz zkbd
bindkey -v
if [[ -f ~/.zkbd/$TERM-${DISPLAY:-$VENDOR-$OSTYPE} ]]; then
    source ~/.zkbd/$TERM-${DISPLAY:-$VENDOR-$OSTYPE}
else
    echo "WARNING: Keybindings may not be set correctly!"
    echo "Execute \`zkbd\` to create bindings."
fi

#################################################################################
# Set some keybindings
#################################################################################
# Bind the keys that zkbd set up to some widgets
[[ -n "${key[Home]}" ]]    && bindkey "${key[Home]}"    beginning-of-line
[[ -n "${key[End]}" ]]     && bindkey "${key[End]}"     end-of-line
[[ -n "${key[Insert]}" ]]  && bindkey "${key[Insert]}"  overwrite-mode
[[ -n "${key[Delete]}" ]]  && bindkey "${key[Delete]}"  delete-char
[[ -n "${key[Up]}" ]]      && bindkey "${key[Up]}"      up-line-or-search
[[ -n "${key[Down]}" ]]    && bindkey "${key[Down]}"    down-line-or-search
[[ -n "${key[Left]}" ]]    && bindkey "${key[Left]}"    backward-char
[[ -n "${key[Right]}" ]]   && bindkey "${key[Right]}"   forward-char
[[ -n "${key[C-Up]}" ]]    && bindkey "${key[C-Up]}"    history-beginning-search-backward
[[ -n "${key[C-Down]}" ]]  && bindkey "${key[C-Down]}"  history-beginning-search-forward
[[ -n "${key[C-Left]}" ]]  && bindkey "${key[C-Left]}"  backward-word
[[ -n "${key[C-Right]}" ]] && bindkey "${key[C-Right]}" forward-word
[[ -n "${key[C-R]}" ]]     && bindkey "${key[C-R]}"     history-incremental-search-backward
[[ -n "${key[C-T]}" ]]     && bindkey "${key[C-T]}"     history-incremental-search-forward

export KEYTIMEOUT=1

bindkey -v "^g" beginning-of-line
bindkey -v "^G" end-of-line
bindkey -v "^K" kill-line
bindkey -v "^L" clear-screen
bindkey -v "^r" history-incremental-search-backward
bindkey -v "^U" kill-whole-line
bindkey -v "^V" quoted-insert
bindkey -v "^w" backward-kill-word
bindkey '^?' backward-delete-char
bindkey '^h' backward-delete-char

bindkey -v "^N" down-history
bindkey -v "^P" up-history

bindkey -v "^[H" run-help
bindkey -v "^[h" run-help

# Prepend "doas ". This does not have a Vim parallel.
bindkey "^S" prepend-doas

# Prepend "vim ". This does not have a Vim parallel.
bindkey "^V" prepend-vim

# alow for editing of the command line
# http://zshwiki.org/home/examples/zlewidgets
autoload -U     edit-command-line
zle -N          edit-command-line
bindkey -v '^E' edit-command-line

# ^L to pipe through $PAGER and execute
bindkey -s '^P' "|$PAGER\n"

bindkey ' ' magic-space    # also do history expansion on space

# Show dots while waiting to complete. Useful for systems with slow net access,
# like those places where they use giant, slow NFS solutions. (Hint.)
# expand-or-complete-with-dots() {
#   echo -n "\e[31m......\e[0m"
#   zle expand-or-complete
#   zle redisplay
# }
# zle -N expand-or-complete-with-dots
# bindkey "^I" expand-or-complete-with-dots

# This inserts a tab after completing a redirect. You want this.
# (Source: http://www.zsh.org/mla/users/2006/msg00690.html)
self-insert-redir() {
  integer l=$#LBUFFER
  zle self-insert
  (( $l >= $#LBUFFER )) && LBUFFER[-1]=" $LBUFFER[-1]"
}
zle -N self-insert-redir
for op in \| \< \> \& ; do
    bindkey "$op" self-insert-redir
done

#https://github.com/paradigm/dotfiles/blob/master/.zshrc
# Prepend "doas" to the command line if it is not already there.
prepend-doas() {
    if ! echo "$BUFFER" | grep -q "^doas "; then
        BUFFER="doas $BUFFER"
        CURSOR+=5
    fi
}
zle -N prepend-doas

# Prepend "vim" to the command line if it is not already there.
prepend-vim() {
  if ! echo "$BUFFER" | grep -q "^vim "; then
    BUFFER="vim $BUFFER"
    CURSOR+=5
  fi
}
zle -N prepend-vim


# bind UP and DOWN arrow keys
for keycode in '[' 'O'; do
  bindkey "^[${keycode}A" history-substring-search-up
  bindkey "^[${keycode}B" history-substring-search-down
done
unset keycode

# bind k and j for VI mode
bindkey -M vicmd 'k' history-substring-search-up
bindkey -M vicmd 'j' history-substring-search-down

bindkey '^n' autosuggest-accept

@agkozak agkozak self-assigned this Dec 15, 2018
@agkozak
Copy link
Owner

agkozak commented Dec 15, 2018

Cool dotfiles! I'll have to borrow some things from those.

Interestingly, I can run zsh -f and then source both of those files and zsh-z and everything is perfectly perfect. It must be some other part of your .zshrc and the files it sources. I recommend that you try commenting out all of the files your .zshrc includes using . or source and then see if zsh-z completion works. If it does, you can start uncommenting the various files one by one or in groups and try to locate the one that's causing the problem. I'd be most grateful if you can do that, as I'd like to know what sort of settings people have that could conflict with how I've set the plugin up and anticipate them as much as possible.

@agkozak
Copy link
Owner

agkozak commented Dec 16, 2018

I've just discovered that there's a similar problem with tab completion not working with the zgen and zplug frameworks. I'll get to the bottom of this and let you know what I find out.

@agkozak
Copy link
Owner

agkozak commented Dec 16, 2018

I've created a branch called completions that now works with zgen but not yet with zplug. You may try it if you like. I'll keep you updated.

@agkozak
Copy link
Owner

agkozak commented Dec 16, 2018

I've got the changes on the master branch now. See if they help you.

@ghost
Copy link
Author

ghost commented Dec 16, 2018

I do use zplug, I just updated and it's still not working how it should. I'll try to find some time to debug this myself soon. Thanks for your help.

@agkozak
Copy link
Owner

agkozak commented Dec 16, 2018

I found that, after I added

zplug "agkozak/zsh-z"

to my .zshrc, I had to run

zplug install
zplug load

@ghost
Copy link
Author

ghost commented Dec 19, 2018

Sorry for the delays on my response, I been super busy. I tracked down to my ~/.zsh/options.zsh file. Then down to one specific setting from there.

Enabling this line stops z from tab completing.

setopt complete_aliases

Not a big deal to me if I have it on so I just turned it off for now. Thanks for your help. Maybe you can get things so they will work together in the future.

@ghost
Copy link
Author

ghost commented Dec 19, 2018

Figured it may be useful to see the rest of my options to too so you can see what might be being used in the wild?

#!/bin/zsh -f

typeset -ga preexec_functions
typeset -ga precmd_functions
typeset -ga chpwd_functions

# options that should be mostly pretty agreeable

# no c-s/c-q output freezing
 setopt noflowcontrol
 # allow expansion in prompts
 setopt prompt_subst
 # this is default, but set for share_history
     setopt append_history
     # save each command's beginning timestamp and the duration to the history file
     setopt extended_history
     # display PID when suspending processes as well
     setopt longlistjobs
     # try to avoid the 'zsh: no matches found...'
     setopt nonomatch
     # report the status of backgrounds jobs immediately
     setopt notify
     # whenever a command completion is attempted, make sure the entire command path
     # is hashed first.
     setopt hash_list_all
     # not just at the end
     setopt completeinword
     # use zsh style word splitting
     setopt noshwordsplit
     # allow use of comments in interactive code
     setopt interactivecomments

     if [[ -z "$ZSH_SANEOPT_INSANITY" ]]; then
         ZSH_SANEOPT_INSANITY=1
     fi

     if [[ "$ZSH_SANEOPT_INSANITY" -gt 0 ]]; then
         # in order to use #, ~ and ^ for filename generation grep word
             # *~(*.gz|*.bz|*.bz2|*.zip|*.Z) -> searches for word not in compressed files
                 # don't forget to quote '^', '~' and '#'!
                 setopt extended_glob

     # don't error out when unset parameters are used
     setopt unset
 fi

 setopt share_history
 setopt histignorespace

 # Don't send SIGHUP to background processes when the shell exits.
 setopt nohup
 # make cd push the old directory onto the directory stack.
 setopt auto_pushd
 # avoid "beep"ing
 setopt nobeep
 # don't push the same dir twice.
 setopt pushd_ignore_dups

 setopt prompt_subst
 setopt auto_name_dirs
 setopt pushdminus
 setopt auto_param_slash
 setopt vi
 setopt auto_list

    ### change directory
    setopt auto_cd

    ### enable command correction
    setopt correct
    #setopt correct_all

    ### prevent file overwrite
    setopt no_clobber

    ### pound sign in interactive prompt
    setopt interactive_comments

    ### superglobs
    unsetopt case_glob

    ### expansions performed in prompt
    setopt prompt_subst
    setopt magic_equal_subst

    ### prompt about background jobs on exit
    setopt check_jobs

    ### notify on job complete
    setopt notify

    ### disable beep
    unsetopt beep
    setopt no_beep

    ### automatic rehash
    setopt nohashdirs

    #setopt complete_in_word
    setopt always_to_end

    ### complete aliases
    #setopt complete_aliases

    if [[ ${ZSH_VERSION//\./} -ge 420 ]] ; then
        autoload -U url-quote-magic
        zle -N self-insert url-quote-magic
    fi
# vim: set et sw=4 sts=4 ts=4 ft=zsh :

@agkozak
Copy link
Owner

agkozak commented Dec 19, 2018

Thank you! This is exactly what I needed.

Try typing

compdef z=zshz

at the command line and see if that fixes the problem. If it does, we'll go from there.

@ghost
Copy link
Author

ghost commented Dec 19, 2018

It does fix it for that session. Soon as I fire up a new terminal it breaks again.

@agkozak
Copy link
Owner

agkozak commented Dec 19, 2018

OK - I just pushed a new commit with a fix for that. Give it a try.

@ghost
Copy link
Author

ghost commented Dec 19, 2018

I just did zplug update it pulled in the update, but when that setopt is on it still doesn't work.

@agkozak
Copy link
Owner

agkozak commented Dec 19, 2018

Here's what worked for me experimenting with zplug. I changed my .zshrc to setopt COMPLETE_ALIASES, just like yours, and completion with z didn't work. I ran zplug update, just like you -- still no luck. Then I ran

zplug clear
zplug load

to clear zplug's cache and load the plugins afresh. That worked. Give it a try and let me know if it worked for you.

For the record, here's what's going on. When you use setopt COMPLETE_ALIASES, you're telling ZSH that for the purposes of completion, you don't want to identify aliases with the commands they're aliases for. In the case of ZSH-z, it's zshz that's the real command, and z is an alias for it that can be redefined by setting ZSHZ_CMD to something before you load the plugin. In your case, though, that alias would never be completed.

What I've done as a workaround

is to change

compdef _zshz zshz

which tells the completion system to use the completions in the _zshz file for the function `zsh, to

compdef _zshz zshz ${ZSHZ_CMD:-${_Z_CMD:-z}}

which says that, in addition to being completions for zshz, _zshz is explicitly the completion file for whatever alias is being used, by default z.

@ghost
Copy link
Author

ghost commented Dec 19, 2018

I ran the whole zplug clear and zplug load thing now it's resolved. Thanks for all the work you put into this. Great little plugin.

@ghost ghost closed this as completed Dec 19, 2018
@agkozak
Copy link
Owner

agkozak commented Dec 19, 2018

Thanks so much for patiently trying my suggestions. You were very helpful.

This issue was closed.
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

1 participant