skywind3000 / z.lua

:zap: A new cd command that helps you navigate faster by learning your habits.
MIT License
3k stars 141 forks source link

Example function "zii" from wiki doesn't work #82

Open ttrei opened 4 years ago

ttrei commented 4 years ago

https://github.com/skywind3000/z.lua/wiki/Effective-with-fzf#define-a-new-z--i

function zii() {
    local $dir="$(z -l "$@"|fzf --nth 2.. --reverse --inline-info --tac +s -e --height 35%)"
    [ -n "$dir" ] && cd "$dir"
}

In bash it fails like this:

bash: local: `=134         /var/tmp': not a valid identifier

This works for me:

function zii() {
    local dir="$(z -l "$@"|fzf --nth 2.. --reverse --inline-info --tac +s -e --height 35%)"
    [ -n "$dir" ] && cd "$(echo $dir | tr -s ' ' | cut -d ' ' -f 2)"
}
hyq5436 commented 3 years ago

This works for me:

function zii() { local dir="$(z -l "$@"|fzf --nth 2.. --reverse --inline-info --tac +s -e --height 35%)" [ -n "$dir" ] && cd "$(echo $dir | tr -s ' ' | cut -d ' ' -f 2)" }

skywind3000 commented 3 years ago

Thanks , fixed with a better version:

function zii() {
    local $dir="$(z -l "$@"|fzf --nth 2.. --reverse --inline-info --tac +s -e --height 35%)"
    [ -n "$dir" ] && cd "$(echo $dir | sed -e 's/^\S*\s*//')"
}

Because tr -s ' ' | cut -d ' ' -f 2 will fail when dir names contain spaces. sed is accurate and safe.