Autojump2/autojump2.bash

70 lines
1.8 KiB
Bash
Raw Normal View History

2012-01-23 16:31:04 +01:00
# Copyright Grégory Soutadé 2012
# This file is part of autojump2
# autojump2 is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# autojump2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with autojump2. If not, see <http://www.gnu.org/licenses/>.
# Original code has been written by Joel Schaerer (https://github.com/joelthelion/autojump)
_autojump()
{
# No arguments or begining of a local directory --> directory completion
[ $COMP_CWORD -eq 0 ] && return
for i in ${COMP_WORDS[$COMP_CWORD]}* ; do
[ -d "$i" ] && return
done
local cur
cur=${COMP_WORDS[*]:1}
while read i
do
COMPREPLY=("${COMPREPLY[@]}" "${i}")
done < <(autojump2 --bash --completion $cur)
}
complete -o dirnames -F _autojump cd
complete -F _autojump autojump2
function j {
new_path=""
# No args, goto home
if [ $# -eq 0 ] ; then
\cd >/dev/null || return
new_path="$(pwd -P)"
else
case "$1" in
# Handle relative paths
"-"|"\.\.*"|"/*"|"~.*")
\cd "$1" && new_path="$(pwd -P)" || return
;;
*)
if [ ! -d "$1" ] ; then
new_path="$(autojump2 $@)"
if [ -n "$new_path" ]; then
\cd "$new_path" || return
echo -e "\\033[31m${new_path}\\033[0m"
return
fi
fi
;;
esac
fi
# Classic cd behaviour
if [ -z "$new_path" -a -d "$1" ] ; then
\cd "$1" || return
fi
}