-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbash_functions
138 lines (119 loc) · 2.9 KB
/
bash_functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
gm() {
mod="$1"
# goto module directory
module_path=$(python3 -c "import $mod;print($1.__file__.rsplit('/', 1)[0])" 2>/dev/null)
cd $module_path
}
ls() {
cwd=$(pwd)
OPTIONS="-lhtr"
if [ "x$cwd" = "x$HOME" ]; then
OPTIONS="--hide='VirtualBox VMs' --hide=Applications --hide=Books --hide=Desktop --hide=Dropbox --hide=Library --hide=Music --hide=Movies --hide=Pictures --hide=Public --hide=Documents $OPTIONS"
fi
if [ "x$ARCH" = "xDarwin" ]; then
bin="gls"
else
bin="/bin/ls"
fi
echo $OPTIONS | xargs "$bin" --color=always $*
}
mk() {
if [ $# -eq 0 ]; then
eval "$MARKED"
else
export MARKED="$@"
echo "command marked."
fi
}
ip138() {
curl "http://www.ip138.com/ips138.asp?ip=$1" 2>/dev/null | iconv -f gb18030 -t utf-8 | egrep "\"ul1" | sed "s/<[^>]*>/./g;s/^\s*//g; s/\.\././g;s/\.\././g" | python -c "import sys; print sys.stdin.read().replace('.', '\n')" | grep -v "^$"
}
gc() {
if git config remote.origin.url | grep -q github.com; then
git commit -vs --author "Kai Xia <kaix+github@fastmail.com>" "$@"
else
git commit -v --author "Kai Xia <${ALT_GIT_EMAIL}>" "$@"
fi
}
vm() {
# toggle vi edit mode in bash.
current_mode=$(bind -v | awk '/keymap/ {print $NF}')
if [ "$current_mode" = "emacs" ]; then
set -o vi
else
set -o emacs
fi
}
aws-extract() {
# export values in ~/.aws/credentials
while IFS= read -r line; do
name=$(echo "$line" | awk '{print $1}' | tr '[:lower:]' '[:upper:]')
value=$(echo "$line" | awk '{print $3}')
eval "export $name=\"$value\""
done < <(cat ~/.aws/credentials | grep -A 3 "\[default\]" | tail -n 3)
}
cat-dir() {
local dir="$1"
# Use current working directory if no directory is provided
if [[ -z "$dir" ]]; then
dir=$(pwd)
fi
if [[ ! -d "$dir" ]]; then
echo "Error: '$dir' is not a valid directory."
return 1
fi
# Find all files in the directory and print each file with its name
find "$dir" -type f | while read -r file; do
echo "File: $(basename "$file")"
cat "$file"
echo "--------------------"
done
}
cd () {
if [ "$#" = 0 ]
then
builtin cd
return
fi
# bookmarked dirs.
case "$1" in
"=e")
builtin cd ~/.xiaket/etc
return
;;
"=g")
builtin cd "$(git rev-parse --show-toplevel)"
return
;;
esac
# dest is a file, go to its dir
if [[ -f "$1" ]]
then
dir_name="$(dirname $1)"
builtin cd "$dir_name"
return
fi
if [[ -d "$1" ]] || [[ $1 == */* ]] || [[ $1 == "-" ]]
then
# default cd behavior
builtin cd "$1"
return
fi
found=$(find . -name "$1" -type d)
# Check if the output is empty
if [ -z "$found" ]; then
matches=0
else
matches=$(echo "$found" | grep -c '^')
fi
if [ "$matches" = "0" ]
then
# fallback to default cd behavior, but this would probably fail
builtin cd "$@"
elif [ "$matches" = "1" ]
then
builtin cd "$found"
else
echo -e "found $matches dirs:\n\n$found"
fi
}