-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalias.sh
149 lines (131 loc) · 2.81 KB
/
alias.sh
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
139
140
141
142
143
144
145
146
147
148
149
alias ..=up
alias ...='cd ../..'
alias 664='chmod 664'
alias 775='chmod 775'
alias 775+664='find . -type d -exec chmod 775 {} + ; find . -type f -exec chmod 664 {} +'
alias apt-all='sudo bash -c "apt update && apt upgrade && apt autoremove && apt clean"'
alias dfh='df -H'
alias dussi='du -s --si'
alias l=less
alias ll='ls -l'
alias lr='ls -ltr'
alias md='mkdir'
alias rp='realpath'
alias vip='vi -p'
alias xon='xdg-open "$(newest)"'
type2vi() {
if [ $# -ne 1 ]; then
echo >&2 "Exactly one argument must be given."
return 1
fi
local file=$(type -p "$1")
if [ -n "$file" ]; then
vi "$file"
else
type "$1"
fi
}
mcd() {
if [[ $# -eq 1 ]]; then
mkdir -p "$1" && cd "$1"
else
echo >&2 "${FUNCNAME[0]}: exactly one argument required."
return 1
fi
}
up() {
local levels=1
if [[ $# -ne 0 ]]; then
if [[ $1 =~ ^[0-9]+$ ]]; then
levels=$1
else
echo >&2 "${FUNCNAME[0]}: argument must be a non-negative integer."
return 1
fi
fi
local dir=""
while (( levels-- )); do
dir="../$dir"
done
[[ -n $dir ]] && cd "$dir"
}
vir() {
if [[ $# -eq 0 ]]; then
vi -R -
else
vi -Rp "$@"
fi
}
xo() {
for file; do
xdg-open "$file"
done
}
inc() {
local increment=${1:-1}
local here=$(pwd)
local base=""
local number=$here
if [[ $here =~ ^(.*[^0-9])([0-9]+)$ ]]; then
base=${BASH_REMATCH[1]}
number=${BASH_REMATCH[2]}
fi
cd "$base$(( number + increment ))"
}
alias cd+=inc
dec() {
local decrement=${1:-1}
inc "-$decrement"
}
strlen() {
echo ${#1}
}
# cd with some extended functionality
_hwcd() {
local targetdir
# One argument
if [ $# -eq 1 ]; then
# If it starts with '-', call the builtin
# (no need to implement option handling here).
if [[ "$1" == -* ]]; then
builtin cd "$@"
return
fi
# If the argument is a *file*,
# cd to the parent directory.
if [ -f "$1" ]; then
targetdir=$(dirname -- "$1")
printf '%s\n' "$targetdir"
builtin cd "$targetdir"
return
fi
# If the argument
# - is a relative path
# - and does not exist in $PWD,
# try to find it under the parent, grandparent
# etc. directory.
if [[ "$1" != /* ]] && [ ! -e "$1" ]; then
local parent=$PWD
until [ -z "$parent" ]; do
parent=$(dirname -- "$parent")
[ "$parent" = '/' ] && parent=''
targetdir="$parent/$1"
if [ -e "$targetdir" ]; then
printf '%s\n' "$targetdir"
builtin cd "$targetdir"
return
fi
done
fi
# Two arguments: replace (the first occurrence of) $1
# with $2 in $PWD.
elif [ $# -eq 2 ]; then
targetdir=${PWD/$1/$2}
printf '%s\n' "$targetdir"
builtin cd "$targetdir"
return
fi
# Default behavior: call the builtin.
builtin cd "$@"
}
alias cd=_hwcd