completion for qcd

This commit is contained in:
ville rantanen
2018-11-16 10:52:07 +02:00
parent c4b484a7b9
commit 7b78f12f08

View File

@@ -2,9 +2,9 @@
## improved CD commands: ## improved CD commands:
_QCD_FIND=find _QCD_FIND=find
case $OSTYPE in case $OSTYPE in
darwin*) darwin*)
alias tac=gtac alias tac=gtac
_QCD_FIND=gfind _QCD_FIND=gfind
;; ;;
esac esac
@@ -35,7 +35,7 @@ function hcd() {
d=$( tail -n 20 "$HOME/.bash_cdhistory" | head -n 19 | cut -d: -f2 | tac | cat -n | sed 's,^ \+,,' | grep -h "^$1 " ) d=$( tail -n 20 "$HOME/.bash_cdhistory" | head -n 19 | cut -d: -f2 | tac | cat -n | sed 's,^ \+,,' | grep -h "^$1 " )
d=${d/* /} d=${d/* /}
if [ ! -z "$d" ] if [ ! -z "$d" ]
then \cd "$d" then \cd "$d"
fi fi
} }
@@ -65,8 +65,8 @@ function qcd() {
local opt local opt
local case local case
local d local d
[ -e ~/.qcd ] || touch ~/.qcd [ -e ~/.qcd ] || touch ~/.qcd
[ -e ~/.bash_cdhistory ] || touch ~/.bash_cdhistory [ -e ~/.bash_cdhistory ] || touch ~/.bash_cdhistory
while getopts ae:hiILl:m opt while getopts ae:hiILl:m opt
do case "$opt" in do case "$opt" in
a) a)
@@ -84,32 +84,14 @@ function qcd() {
case="-i" case="-i"
;; ;;
I) I)
echo "See ~/.bash_completion for possible duplicates"
echo '
have qcd &&
_qcd()
{
local cur
local d
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W "-a -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
. /etc/bash_completion
touch ~/.qcd ~/.bash_cdhistory touch ~/.qcd ~/.bash_cdhistory
return return
;; ;;
L) L)
echo ==History ~/.bash_cdhistory: echo '## History ~/.bash_cdhistory ##'
cat ~/.bash_cdhistory cat ~/.bash_cdhistory
echo echo
echo ==Saved ~/.qcd: echo '## Saved ~/.qcd ##'
cat ~/.qcd cat ~/.qcd
return return
;; ;;
@@ -146,7 +128,7 @@ complete -F _qcd qcd
;; ;;
h) h)
echo 'qcd [-hiLm]|[-al] [name] echo 'qcd [-hiLm]|[-al] [name]
Version: 2017-03-24 Version: 2018-11-16
Change current working path based on the list ~/.qcd Change current working path based on the list ~/.qcd
Keeps a history of folders visited in ~/.bash_cdhistory Keeps a history of folders visited in ~/.bash_cdhistory
@@ -155,7 +137,7 @@ Keeps a history of folders visited in ~/.bash_cdhistory
the basename will be used the basename will be used
-e [name] Show the match, store in variable QCD -e [name] Show the match, store in variable QCD
-i Case insensitive search, must come first. -i Case insensitive search, must come first.
-I Install, with autocompletion (use only once) -I Install (use only once)
-l [name] Show the match, but do not change -l [name] Show the match, but do not change
-L Lists the paths -L Lists the paths
-m Maintain the list, deleting non-existing entries' -m Maintain the list, deleting non-existing entries'
@@ -179,10 +161,22 @@ Keeps a history of folders visited in ~/.bash_cdhistory
d=$( grep $case -h ^"$1" ~/.qcd <( tac ~/.bash_cdhistory ) | head -n 1 ) d=$( grep $case -h ^"$1" ~/.qcd <( tac ~/.bash_cdhistory ) | head -n 1 )
d=${d/*:/} d=${d/*:/}
if [ ! -z "$d" ] if [ ! -z "$d" ]
then \cd "$d" then \cd "$d"
fi fi
}
} # qcd ends _qcd()
{
local cur
local d
cur=${COMP_WORDS[COMP_CWORD]}
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W "-a -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
# qcd ends
function ncd() { function ncd() {
# echo Next sibling cd # echo Next sibling cd
@@ -201,7 +195,7 @@ function ncd() {
fi fi
if [ "$_iter_d" = "$_current_pwd" ]; then if [ "$_iter_d" = "$_current_pwd" ]; then
_current_found=true _current_found=true
if [ "$1" = "-" ]; then if [ "$1" = "-" ]; then
if [ -z "$_iter_prev" ]; then if [ -z "$_iter_prev" ]; then
echo $_current_pwd was the first folder. echo $_current_pwd was the first folder.
else else
@@ -226,7 +220,7 @@ function scd() {
return return
} }
local cdto cdfrom local cdto cdfrom
[[ -z "$2" ]] && { [[ -z "$2" ]] && {
cdfrom="." cdfrom="."
} || { } || {
cdfrom="$2" cdfrom="$2"
@@ -243,7 +237,7 @@ function foldermenu_prompt {
# Function to add in PS prompt variable, enabling foldermenu support # Function to add in PS prompt variable, enabling foldermenu support
[ -f .foldermenu ] && { [ -f .foldermenu ] && {
foldermenu -lf 10 $@ || echo -n "*" foldermenu -lf 10 $@ || echo -n "*"
} || { } || {
true true
} }
} }
@@ -252,7 +246,7 @@ function igrep () {
# Interactive grep, read grep arguments # Interactive grep, read grep arguments
[[ "$1" = "-h" ]] && [[ "$#" -eq 1 ]] && { [[ "$1" = "-h" ]] && [[ "$#" -eq 1 ]] && {
echo Interactive grep, read grep arguments echo Interactive grep, read grep arguments
return return
} }
local args local args
args="-i -e " args="-i -e "
@@ -271,7 +265,7 @@ function ised () {
# Interactive sed # Interactive sed
[[ "$1" = "-h" ]] && [[ "$#" -eq 1 ]] && { [[ "$1" = "-h" ]] && [[ "$#" -eq 1 ]] && {
echo Interactive sed, read sed arguments echo Interactive sed, read sed arguments
return return
} }
local args local args
if [ ! -f "$1" ]; then echo must give atleast one file; return 1;fi; if [ ! -f "$1" ]; then echo must give atleast one file; return 1;fi;
@@ -315,9 +309,9 @@ function rmv () {
function whenfilechanges() { function whenfilechanges() {
# Run a command when file(s) time stamp changes # Run a command when file(s) time stamp changes
echo "Version: 2015-10-13" echo "Version: 2015-10-13"
[ -z "$2" ] && { echo 'Usage: whenfilechanges "file" "some" "command" [ -z "$2" ] && { echo 'Usage: whenfilechanges "file" "some" "command"
Follows the modification time of the "file" and runs the command Follows the modification time of the "file" and runs the command
at every change. at every change.
The "file" may also be a glob e.g. "*tex" ' The "file" may also be a glob e.g. "*tex" '
return return
} }
@@ -327,7 +321,7 @@ function whenfilechanges() {
local otime local otime
local ntime local ntime
local i local i
fname=($1) fname=($1)
echo Waiting for ${#fname[@]} files to change: ${fname[@]} echo Waiting for ${#fname[@]} files to change: ${fname[@]}
shift 1 shift 1
@@ -339,24 +333,23 @@ function whenfilechanges() {
return return
} }
otime[$i]=$( stat -c %Z "${fname[$i]}" ) otime[$i]=$( stat -c %Z "${fname[$i]}" )
done
while :
do ntime=()
for ((i=0;i<${#fname[@]};i++))
do ntime[$i]=$( stat -c %Z "${fname[$i]}" )
[ "${ntime[$i]}" -ne "${otime[$i]}" ] && {
echo "${fname[$i]} changed:"
otime[$i]=$( stat -c %Z "${fname[$i]}" )
eval "$@"
echo -n "Waiting for file changes... "
date
}
done
echo -ne Waiting.. $( date )\\r
read -t 2 forced
[ $? -eq 0 ] && eval "$@"
done done
while :; do
ntime=()
for ((i=0;i<${#fname[@]};i++)); do
ntime[$i]=$( stat -c %Z "${fname[$i]}" )
[[ "${ntime[$i]}" -ne "${otime[$i]}" ]] && {
echo "${fname[$i]} changed:"
otime[$i]=$( stat -c %Z "${fname[$i]}" )
eval "$@"
echo -n "Waiting for file changes... "
date
}
done
echo -ne Waiting.. $( date )\\r
read -t 2 forced
[ $? -eq 0 ] && eval "$@"
done
} }
# Shell enhancement # Shell enhancement
@@ -397,7 +390,7 @@ function path_add_current {
return return
} }
export PATH="${p}:$PATH" export PATH="${p}:$PATH"
path_remove_duplicates path_remove_duplicates
echo PATH=$PATH echo PATH=$PATH
} }
function path_remove_duplicates { function path_remove_duplicates {