wting / autojump

A cd command that learns - easily navigate directories from the command line
Other
16.2k stars 706 forks source link

Cleaner zsh completion #490

Open jamesk opened 7 years ago

jamesk commented 7 years ago

Hi, love autojump, I've just started using it with zsh (and oh-my-zsh) and I had what seemed like a bug in the completion, duplicate folder entries. The completion also automatically added __ to my command as all results from autojump -complete start with the search term and __.

Anyway, posting this in case it affects anyone else / you want to incorporate it into autojump. This is my modified _j file:

#compdef j
cur=${words[2, -1]}

integer i=1
declare -A displayMap

autojump --complete "${=cur[*]}" | while read c; do
    hidden=$(echo "$c" | sed 's/\(.*__[0-9][0-9]*__\).*/\1/')
    display=$(echo "$c" | sed 's/.*__[0-9][0-9]*__\(.*\)/\1/')

    (( $+displayMap[$display] )) && continue
    displayMap[$display]=true

    compadd -V $i -U -i "$hidden" "$display";
    i=$((i+1))
done

(( $i > 2 )) && compadd -V $i -U "${=cur[*]}"

Modifications:

fdw commented 5 years ago

I would love to have something like this :)

However, while trying this script out, I noticed that the chosen completion looks far from clean: <What I entered>__<int>__<actual completion>. Since I use the autocompletion for zsh and the history extensively, this is not ideal. Do you have an idea how to fix it so the final completion is just <actual completion>?

mesmerx commented 4 years ago

@fdw just change the /usr/share/zsh/site-fucntions/_j

to that

compdef j

cur=${words[2, -1]}

integer i=1 declare -A displayMap

autojump --complete "${=cur[]}" | while read c; do hidden=$(echo "$c" | sed 's/(.[0-9][0-9]*)./\1/') display=$(echo "$c" | sed 's/.[0-9][0-9]*(.*)/\1/')

(( $+displayMap[$display] )) && continue
displayMap[$display]=true

compadd -V $i -U "$display";
i=$((i+1))

done

(( $i > 2 )) && compadd -V $i -U "${=cur[*]}"

and you should be good to go