Z autocompletion cp target directory

Viewed 102

It has been a while since I started using the z command of zsh: its frecency-based features are very useful! I am now looking for a similar auto-completion in order to complete the target when using cp:

Expected: behavior:

$: ls
-> foo.txt bar.txt
$: cp + TAB
-> suggests iteratively foo.txt and bar.txt
$: cp foo.txt rece + TAB
-> suggest /home/user/Documents/recent_directory

Is there any way doing so? Thanks for any help!

3 Answers

Yes, there is: Just install zsh-autocomplete.

enter image description here

(Disclaimer: I'm the maintainer of zsh-autocomplete.)

An alternative approach is to use Dynamic named directories http://zsh.sourceforge.net/Doc/Release/Expansion.html#Filename-Expansion .

Suppose you have a function ffz-get that uses fzf to select the recent directory. Then you can:

function zsh_directory_name_1() {
    local type="$1" arg="$2"
    if [[ "$type" == n ]] ; then
        # Dynamic named directories
        # e.g., `ll ~[dl]`

        o="$(ffz-get "$arg")"
        typeset -ga reply
        reply=("$o")
        return 0
    elif [[ "$type" == c ]]; then
        return
    elif [[ "$type" == d ]]; then
        return 1
    else
        echo "$0: Unknown type '$type'"
        return 1
    fi
    return 1
}

typeset -ag zsh_directory_name_functions=()
zsh_directory_name_functions+='zsh_directory_name_1'

You can see my own implementation of ffz-get at https://github.com/NightMachinary/.shells/blob/master/scripts/zshlang/auto-load/others/fuzzy/z.zsh , but you would need to adapt it to use it without using all of my dotfiles.

This whole thing allows you to do your example like so:

cp foo.txt ~[rece
# typing the closing ']' will open `fzf` so that you select the candidate you want
cp foo.txt ~[rece]/bar.txt

Note that my own ffz-get caches user choices for each query (in redis), and I am not sure if this is necessary or not.

I ended up using the following:

$: cp foo.txt $(j rece)
-> copy ./foo.txt to  /home/user/Documents/recent_directory

with j alias for autojump. I assume it would still work for z

Related