mirror of
https://github.com/lloeki/dotfiles.git
synced 2025-12-06 07:24:39 +01:00
kd, to quickly change worKDir
This commit is contained in:
parent
aca96362e5
commit
44696d8d04
1 changed files with 40 additions and 0 deletions
40
shell/kd
Executable file
40
shell/kd
Executable file
|
|
@ -0,0 +1,40 @@
|
|||
# worKDir
|
||||
function kd() {
|
||||
local conf="$HOME/.kdrc"
|
||||
if [[ $# -eq 0 ]]; then
|
||||
local candidate="$PWD"
|
||||
while [[ -n "$candidate" ]]; do
|
||||
for file in Gemfile Procfile; do
|
||||
if [[ -f "$candidate/$file" ]]; then
|
||||
cd "$candidate"
|
||||
return $?
|
||||
fi
|
||||
done
|
||||
candidate="${candidate%/*}"
|
||||
done
|
||||
return 1
|
||||
fi
|
||||
if [[ $# -eq 1 ]]; then
|
||||
local target
|
||||
target="$(grep -e "^$1" "$conf" | awk '{ print $2 }' | tail -1)"
|
||||
if [[ -n "$target" ]]; then
|
||||
cd "$target"
|
||||
return $?
|
||||
fi
|
||||
return 1
|
||||
fi
|
||||
if [[ $# -eq 2 ]]; then
|
||||
local target
|
||||
name="$1"
|
||||
if [[ -d "$2" ]]; then
|
||||
target=$(cd "$2"; pwd)
|
||||
if [[ -n "$target" ]]; then
|
||||
sed -i -e "/^$name/d" "$conf"
|
||||
echo "$name" "$target" >> "$conf"
|
||||
fi
|
||||
fi
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# vim: ft=sh
|
||||
Loading…
Add table
Add a link
Reference in a new issue