Skip to content

Commit 9de6239

Browse files
committed
Initial commit
0 parents  commit 9de6239

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
cdd
2+
===
3+
4+
A simple Bash script to create shortcuts for paths in terminal.
5+
6+
How?
7+
----
8+
9+
1) source /path/to/cdd.sh
10+
11+
2) Type: cdd
12+
13+
Config
14+
------
15+
16+
Shortcuts are stored in ~/.cdd
17+
18+
Usage
19+
-----
20+
21+
cdd add <shortcut> <path>
22+
cdd remove <shortcut>
23+
cdd list [-v]
24+
cdd <shortcut>
25+
26+
Contributing
27+
------------
28+
I'm happy if someone wants to improve this script.
29+
E-mail me at [email protected]
30+
31+
License
32+
-------
33+
34+
This program is free software: you can redistribute it and/or modify
35+
it under the terms of the GNU General Public License as published by
36+
the Free Software Foundation, either version 3 of the License, or
37+
(at your option) any later version.
38+
39+
This program is distributed in the hope that it will be useful,
40+
but WITHOUT ANY WARRANTY; without even the implied warranty of
41+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42+
GNU General Public License for more details.
43+
44+
You should have received a copy of the GNU General Public License
45+
along with this program. If not, see <http://www.gnu.org/licenses/>.
46+

cdd.sh

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env bash
2+
3+
# bash script for creating path shortcuts in terminal
4+
# Copyright (C) 2017 Lars Andre Landås <[email protected]>
5+
# Distributed under the GNU General Public License, version 2.0.
6+
7+
# For usage type cdd in terminal after you have sourced this file
8+
# Includes auto-complete support
9+
# Mac fix: edit line stating with "sed -i ..." to "sed -i '' ..."
10+
11+
if [ ! -f ~/.cdd ]; then
12+
touch ~/.cdd
13+
fi
14+
15+
cdd () {
16+
17+
cdd_array=() # Create array
18+
while IFS= read -r line # Read a line
19+
do
20+
cdd_array+=("$line") # Append line to the array
21+
done < ~/.cdd
22+
23+
remove () {
24+
echo "Removing $1 if exists in ~/.cdd"
25+
sed -i "/^$1 /d" ~/.cdd
26+
}
27+
28+
if [[ $1 = "add" ]] && [[ ! $2 = "" ]]; then
29+
remove $2
30+
echo "$2 $3" >> ~/.cdd
31+
echo "Added $2 $3 to ~/.cdd"
32+
return
33+
fi
34+
35+
if [[ $1 = "remove" ]]; then
36+
remove $2
37+
return
38+
fi
39+
40+
if [[ $1 = "list" ]] && [[ $2 = "-v" ]]; then
41+
sort ~/.cdd
42+
return
43+
fi
44+
45+
46+
if [[ $1 = "list" ]]; then
47+
b=($(for l in "${cdd_array[@]}"; do echo "$l"|cut -d' ' -f1; done | sort))
48+
49+
for l in "${b[@]}"; do
50+
printf "%-8s\n" "${l}"
51+
done | column
52+
53+
return
54+
fi
55+
56+
for line in "${cdd_array[@]}"
57+
do
58+
thea=($line)
59+
IFS=' ' read -r id path <<< "$line"
60+
if [[ $1 == ${id} ]]; then
61+
eval cd "'${path}'"
62+
return
63+
fi
64+
done
65+
66+
if [[ ! $1 == "" ]]; then
67+
echo -e "\e[91m\e[1mError:\e[21m\e[39m $1 is not a shortcut"
68+
fi
69+
70+
echo -e "\e[1mUsage:\e[21m cdd <shortcut> | add <shortcut> path | remove <shortcut> | list [-v]"
71+
}
72+
73+
_cdd() {
74+
local cur cdd_array cdd_args thea
75+
76+
cur=${COMP_WORDS[COMP_CWORD]}
77+
78+
COMPREPLY=()
79+
cdd_array=() # Create array
80+
cdd_args=""
81+
while IFS= read -r line # Read a line
82+
do
83+
cdd_array+=("$line") # Append line to the array
84+
thea=($line)
85+
cdd_args="$cdd_args ${thea[0]}"
86+
done < ~/.cdd
87+
88+
if [ $COMP_CWORD -eq 1 ]; then
89+
COMPREPLY=( $(compgen -W "$cdd_args" -- $cur) )
90+
elif [ $COMP_CWORD -eq 2 ]; then
91+
case "${COMP_WORDS[COMP_CWORD-1]}" in
92+
"remove")
93+
COMPREPLY=( $(compgen -W "$cdd_args" -- $cur) )
94+
;;
95+
"add")
96+
COMPREPLY=( $(compgen -f -- $cur) )
97+
;;
98+
esac
99+
elif [ $COMP_CWORD -eq 3 ]; then
100+
case "${COMP_WORDS[COMP_CWORD-2]}" in
101+
"add")
102+
_filedir
103+
;;
104+
esac
105+
fi
106+
return 0
107+
}
108+
109+
complete -F _cdd cdd

0 commit comments

Comments
 (0)