You are not logged in.

#1 2010-06-13 00:46:16

some-guy94
Member
Registered: 2009-08-15
Posts: 360

Bash bookmarks function + cd, completion problems

This

export BMDB="${XDG_CONFIG_HOME}/bm"
bm()
{
if [[ -z $1 ]]; then
    return 0
elif [[ $1 != '.' || $1 != '..' ]]; then
    echo "$1"
    return 0
fi
[[ -d "$BMDB" ]] || mkdir -p "$BMDB"
case $1 in
    +*)
        if [[ "${2:0:1}" == "/" ]]; then
            echo "$BMDB/${1:1} -> $2"
            ln -s "$2" "$BMDB/${1:1}"
        else
            echo "$BMDB/${1:1} -> $PWD/$2"
            ln -s "$PWD/$2" "$BMDB/${1:1}"
        fi
    ;;
    -*)
        rm -f "$BMDB/${1:1}"
    ;;
    *)
        if [[ -h "$BMDB/$1" ]]; then
            readlink "$BMDB/$1"
        elif [[ -e "$BMDB/$1" ]]; then
            local answer
            read -p "$1 is an invalid bookmark, would you like to delete it? [Y/n] " answer
            [[ $(tr [:upper:] [:lower:] <<<${answer:0:1}) != n ]] && rm -f "$BMDB/$1"
        else
            echo "$1"
            return 1
        fi
    ;;
esac
}

_cd()
{
local path="$(bm "$@")"
if [[ -z $path ]]; then
    builtin cd 
else
    builtin cd "$path"
fi
}
alias cd="_cd"

works fine without problems from my initial testing, the only problem I'm having is that tab-completion is completely broken.

Any ideas on how to fix it?

Offline

#2 2010-06-13 07:58:30

hbekel
Member
Registered: 2008-10-04
Posts: 311

Re: Bash bookmarks function + cd, completion problems

You've redefined _cd(), which is a completion function defined by the bash_completion package, see:

$ complete -p cd
$ type _cd

You don't need the "cd" alias here. Just call the function "cd" instead of "_cd".

Offline

#3 2010-06-13 08:06:49

hbekel
Member
Registered: 2008-10-04
Posts: 311

Re: Bash bookmarks function + cd, completion problems

Btw, you can't rely on XDG_* variables to be defined in the environment. use

BMDB="${XDG_CONFIG_HOME:-$HOME/.config}/bm"

To use the default value $HOME/.config if XDG_CONFIG_HOME is not set or empty.

Offline

#4 2010-06-13 09:40:49

demian
Member
From: Frankfurt, Germany
Registered: 2009-05-06
Posts: 709

Re: Bash bookmarks function + cd, completion problems

I'm sorry to not contribute anything to your function but have you thought about using ZSH? pushd and popd do exactly what you are trying to do. cd - opens a menu of previously visited sites and completion works, too, of course.


no place like /home
github

Offline

#5 2010-06-13 09:51:04

andresp
Member
Registered: 2010-05-29
Posts: 62

Re: Bash bookmarks function + cd, completion problems

some-guy94 wrote:
[[ $(tr [:upper:] [:lower:] <<<${answer:0:1}) != n ]] && rm -f "$BMDB/$1"

Here you want shopt -s nocasematch, or a simplified regex:

[[ answer =~ ^[Nn] ]] || rm -f "$BMDB/$1"

edit:
A more interesting reason behind why you shouldn't use that tr(1) line:

$ touch :; echo [:upper:] [:lower:]
: :

Last edited by andresp (2010-06-13 09:53:28)

Offline

#6 2010-06-13 18:08:12

some-guy94
Member
Registered: 2009-08-15
Posts: 360

Re: Bash bookmarks function + cd, completion problems

Thanks for the replies, it works now.

Offline

Board footer

Powered by FluxBB