Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
74 lines (64 sloc) 1.6 KB
#
# author: WEN Pingbo <wengpingbo@gmail.com>
# date: 2015/04/28
#
# record current dir path
# imitate Windows file explorer behavior(backward, forward)
#
# prevent BASHPID to be expanded
ICD_STACK_S='/tmp/icd.${BASHPID}'
icd_data_init() {
eval ICD_STACK=$ICD_STACK_S
[ ! -f $ICD_STACK ] && echo -e "0\n0" > $ICD_STACK
}
icd_main() {
cd -- "${1:-$HOME}" || return $?
# check interactive shell
[ -z "$PS1" ] && return 0
eval ICD_STACK=$ICD_STACK_S
icd_data_init
local len=$((`cat $ICD_STACK | wc -l` - 2))
local pos=`head -n 2 $ICD_STACK | tail -n 1`
# cut dir stack from pos
eval sed -i \'$((pos + 1 + 2)),$((len + 2))d\' $ICD_STACK
# update len
eval sed -i \'1 s/$len/$((pos + 1))/\' $ICD_STACK
# update pos
eval sed -i \'2 s/$pos/$((pos + 1))/\' $ICD_STACK
# append current dir
echo `pwd` >> $ICD_STACK
}
icd_backward() {
eval ICD_STACK=$ICD_STACK_S
icd_data_init
local len=`head -n 1 $ICD_STACK`
local pos=`head -n 2 $ICD_STACK | tail -n 1`
if [ $pos -gt 1 ];then
eval sed -i \'2 s/$pos/$((pos - 1))/\' $ICD_STACK
cd `head -n $((pos - 1 + 2)) $ICD_STACK | tail -n 1`
return $?
fi
}
icd_forward() {
eval ICD_STACK=$ICD_STACK_S
icd_data_init
local len=`head -n 1 $ICD_STACK`
local pos=`head -n 2 $ICD_STACK | tail -n 1`
if [ $pos -lt $len ];then
eval sed -i \'2 s/$pos/$((pos + 1))/\' $ICD_STACK
cd `head -n $((pos + 1 + 2)) $ICD_STACK | tail -n 1`
return $?
fi
}
icd_finish() {
eval ICD_STACK=$ICD_STACK_S
rm -f $ICD_STACK
}
trap icd_finish EXIT
alias cd=icd_main
alias h=icd_backward
alias l=icd_forward
# Ctrl + right
bind '"\e[1;5D":"h\n"'
# Ctrl + left
bind '"\e[1;5C":"l\n"'
You can’t perform that action at this time.