153 lines
3.6 KiB
Plaintext
153 lines
3.6 KiB
Plaintext
function cd_history () {
|
|
local old
|
|
local p
|
|
local b
|
|
if [ -z "$1" ]
|
|
then \cd "$HOME"
|
|
return
|
|
fi
|
|
old=$( tail -n 149 "$HOME/.bash_cdhistory" )
|
|
echo "$old" > "$HOME/.bash_cdhistory"
|
|
\cd "$1"
|
|
p=$( pwd )
|
|
b=$( basename "$p" )
|
|
echo $b":"$p >> "$HOME/.bash_cdhistory"
|
|
}
|
|
alias cd=cd_history
|
|
|
|
function qcd() {
|
|
local OPTIND
|
|
local OPTARG
|
|
local opt
|
|
local case
|
|
|
|
while getopts aiILl:hm opt
|
|
do case "$opt" in
|
|
a)
|
|
# Adding
|
|
local name=${!OPTIND}
|
|
if [ -z "$name" ]
|
|
then name=$( basename $( pwd ))
|
|
fi
|
|
|
|
echo "$name":$( pwd )
|
|
echo "$name":$( pwd ) >> ~/.qcd
|
|
return
|
|
;;
|
|
i)
|
|
case="-i"
|
|
;;
|
|
I)
|
|
echo "See ~/.bash_completion for possible duplicates"
|
|
echo '
|
|
have qcd &&
|
|
_qcd()
|
|
{
|
|
local cur
|
|
COMPREPLY=()
|
|
cur=${COMP_WORDS[COMP_CWORD]}
|
|
if [[ "$cur" == -* ]]; then
|
|
COMPREPLY=( $( compgen -W '-i -h -l -L -m ' -- $cur ) )
|
|
return 0
|
|
fi
|
|
COMPREPLY=( $( compgen -W "$( grep -h ^"$cur" ~/.qcd <( tac ~/.bash_cdhistory ) | cut -d: -f1 )" ) )
|
|
}
|
|
complete -F _qcd qcd
|
|
' >> ~/.bash_completion
|
|
return
|
|
;;
|
|
L)
|
|
echo ==History ~/.bash_cdhistory:
|
|
cat ~/.bash_cdhistory
|
|
echo
|
|
echo ==Saved ~/.qcd:
|
|
cat ~/.qcd
|
|
return
|
|
;;
|
|
l)
|
|
local d=$( grep $case -h ^"$OPTARG" ~/.qcd <( tac ~/.bash_cdhistory ) | head -n 1 )
|
|
d=${d/*:/}
|
|
echo $d
|
|
return
|
|
;;
|
|
m)
|
|
local IFS=$'\n'
|
|
for line in $( cat ~/.bash_cdhistory );
|
|
do if [ -d "${line/*:/}" ];
|
|
then echo "$line" >> ~/.bash_cdhistory.tmp
|
|
fi
|
|
done
|
|
mv ~/.bash_cdhistory.tmp ~/.bash_cdhistory
|
|
for line in $( cat ~/.qcd );
|
|
do if [ -d "${line/*:/}" ];
|
|
then echo "$line" >> ~/.qcd.tmp
|
|
fi
|
|
done
|
|
mv ~/.qcd.tmp ~/.qcd
|
|
return
|
|
;;
|
|
h)
|
|
echo 'qcd [-hiLm]|[-al] [name]
|
|
Change current working path based on the list ~/.qcd
|
|
Keeps a history of folders visited in ~/.bash_cdhistory
|
|
|
|
-a [name] Adds the path to the list
|
|
You may add the name of the path, but when omitted
|
|
the basename will be used
|
|
-i Case insensitive search, must come first.
|
|
-I Install autocompletion (use only once)
|
|
-l [name] Show the match, but do not change
|
|
-L Lists the paths
|
|
-m Maintain the list, deleting non-existing entries'
|
|
return
|
|
;;
|
|
esac
|
|
done
|
|
shift $(($OPTIND - 1))
|
|
unset OPTSTRING
|
|
unset OPTIND
|
|
if [ -z "$1" ]
|
|
then \cd; return
|
|
fi
|
|
if [ "$1" = "-" ]
|
|
then \cd -; return
|
|
fi
|
|
d=$( grep $case -h ^"$1" ~/.qcd <( tac ~/.bash_cdhistory ) | head -n 1 )
|
|
d=${d/*:/}
|
|
if [ ! -z "$d" ]
|
|
then \cd "$d"
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function whenfilechanges() {
|
|
[ -z "$1" ] && { echo 'Usage: whenfilechanges "file" "some" "command"
|
|
Follows the modification time of the "file" and runs the command
|
|
at every change. '
|
|
return
|
|
}
|
|
[ -e "$1" ] || { echo $1 not found
|
|
return
|
|
}
|
|
local fname
|
|
local otime
|
|
local ntime
|
|
|
|
fname=$1
|
|
shift 1
|
|
otime=$( stat -c %Z "$fname" )
|
|
while :
|
|
do ntime=$( stat -c %Z "$fname" )
|
|
[ "$ntime" -ne "$otime" ] && {
|
|
eval "$@"
|
|
otime=$( stat -c %Z "$fname" )
|
|
}
|
|
|
|
sleep 2
|
|
done
|
|
|
|
}
|
|
|