Tinkering around with something today, cause I end up in directories like:
/client/company1/archive/in/837
wanting to go to
/client/company2/archive/in/837
I wrote it a few ways - so far I think I like this the best. So yeah, with this function in my .bashrc I can say
s company1 company2
to sed pwd and do that directory hop.
# cd with sed
# example:
# /usr/lib/foo$ s lib src
# /usr/src/foo$
s() {
local cd="$PWD"
if [ "$1" = "--complete" ]; then
awk -v q="${2/s /}" -v p="$PWD" '
BEGIN {
split(p,a,"/")
for( i in a ) if( a[i] && tolower(a[i]) ~ tolower(q) ) print a[i]
}
'
else
while [ $1 ]; do
cd="$(echo $cd | sed "s/$1/$2/")"
shift; shift
done
shopt -s cdspell
cd $cd
shopt -u cdspell
fi
}
complete -C 's --complete "$COMP_LINE"' s
It takes pairs of arguments and seds arg to arg+1. The shopt stuff fixes some typos - I dunno why I don't like it enabled all the time - but I feel like it has a bit more panache than just a [ -d "$cd" ] && cd $cd.
(Updated to add tab completion)
Little function I stuck in my bashrc, maybe it'll be useful. Uses the .viminfo file to get a list of recently edited files. Obiously not much use unless you use vim as your One True Editor.
vl [-] # edit most recently edited file
vl [-] 0 # equivalent to above
vl [-] 3 # 4th last edited file (we're 0 indexed)
If $1 is -, show what we'd do but don't do it.
vl() {
[ "$1" == '-' ] && shift && local echo=echo
fl=$(awk -v h="$HOME" -v q=${1:-0} '
$1 == ">" {
if ( system("test -f " $2) ) next
if( y++ != q ) next
sub(/^~/,h,$2)
print $2
exit
}
' .viminfo)
[ "$fl" ] || return
$echo vim $fl
}
" When editing a file, always jump to the last cursor position
autocmd BufReadPost *
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\ exe "normal g'\"" |
\ endif