-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.shell_aliases
307 lines (268 loc) · 7.84 KB
/
.shell_aliases
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Text editing
export EDITOR='nvim'
alias edit='${=EDITOR} $@'
alias sedit='sudo -E edit'
alias e='edit'
alias se='sedit'
export VISUAL='code'
# Quick file edits
alias zshrc='${=EDITOR} ~/.zshrc'
alias vimc='${=EDITOR} ~/.config/nvim/lua/user'
arc() {
local files=(
"$HOME/.oh-my-zsh/custom/profile.zsh"
"$HOME/.shell_aliases"
"$HOME/github/dotfiles/.shell_aliases"
"$HOME/code/dotfiles/.shell_aliases"
)
for file in "${files[@]}"; do
# Check if directory exists and we have read+write permissions
if [[ -r "$file" ]] && [[ -w "$file" ]]; then
cd "$(dirname "$file")" && $VISUAL "$file"
elif [[ -e "$file" ]]; then
sudo -E $EDITOR "$file"
fi
done
}
nic() {
local dirs=(
"$HOME/github/nixdots"
"$HOME/code/nixdots"
"$HOME/nixdots"
"/etc/nixos/nixdots"
)
for dir in "${dirs[@]}"; do
# Check if directory exists and we have read+write permissions
if [[ -r "$dir" ]] && [[ -w "$dir" ]]; then
cd "$dir" && $VISUAL "$dir"
elif [[ -d "$dir" ]]; then
sudo -E $EDITOR "$dir"
fi
done
}
# Change default commands
alias rm='rm -iv'
alias cp='cp -v'
alias mv='mv -v'
alias mkdir='mkdir -p'
alias ping='ping -c 10'
alias tree='tree -CAF --dirsfirst'
#alias ps='ps auxf'
# Shorthand commands
alias jc='journalctl'
alias sc='systemctl'
alias scf='systemctl list-units --failed'
alias rhit='systemctl reboot'
alias shit='systemctl poweroff'
alias j='just'
alias treed='tree -d'
alias cd..='cd ..'
alias cd...='cd ...'
alias cd....='cd ....'
# Logging / printing functions
# Quick less man:
# -R raw control characters (only ANSI and OSC 8 sequences are output in raw form)
# -F quit if one screen or less height
# -K quit on interrupt (Ctrl+C)
# -X disable sending termcap init and deinit strings to the terminal (sometimes used if a program does a weird deinit)
# -S chop long lines instead of wrapping them (this enables paging with the right arrow key to see the long lines)
# -M causes less to prompt more verbosely
# -s squeeze blank lines (don't show repeating newlines)
# -c clear screen, then show stuff
export LESS='-RKF'
export MANPAGER="sh -c 'col -bx | bat -pl man'"
export MANROFFOPT="-c"
# export SYSTEMD_LESS="RKFXSM"
alias bat='bat -ppl log'
# Usage: <command> | h
alias h='bat -l help'
# Follows the file's output, showing a live log
alias lgf="tail -f -n100 $1 | bat"
# Follow a systemd unit's output
function jlgf() {
journalctl -u $@ -f --lines=100 --no-pager | bat
}
# Opens the file, goes to the bottom, interactive so you can navigate it
alias lg='bat --paging=auto --pager="less +G" -pl log $@'
# Open a systemd unit's log
function jlg() {
journalctl --no-pager -u $@ | bat --pager='less +G'
}
# Show disk space
alias duff='duf -only-mp "/mnt*"'
alias dff='df --output=target,source,fstype,size,used,avail,pcent -h | (sed -u 1q; sort -k 1,1)'
alias dfa='du -h -d1 | sort -hr'
immich() {
case $1 in
up) sudo systemctl start docker-immich-{ml,server,microservices,postgres,redis} ;;
down) sudo systemctl stop docker-immich-{ml,server,microservices,postgres,redis} ;;
*) echo "type down or up" ;;
esac
}
# Allows aliases to be used after sudo
alias sudo='sudo '
alias adm='sudo -s'
# Convert all files in current directory recursively to LF line endings
alias dr='find . -type f -print0 | xargs -0 dos2unix'
# Remove a directory and all files
alias rmd='rm --recursive --force --verbose '
# Neovim's swap directory which annoys me sometimes
alias vds='rm -f ~/.local/state/nvim/swap/*'
# Top 10 processes by CPU usage
alias topcpu="/bin/ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10"
# Show open ports
alias openports='netstat -nape --inet'
# Count all files (recursively) in the current folder
alias countfiles="for t in files links directories; do echo \`find . -type \${t:0:1} | wc -l\` \$t; done 2> /dev/null"
# alias ls="ls -AFh --color=always" # add colors and file type extensions
# alias lx="ls -lXBh" # sort by extension
# alias lk="ls -lSrh" # sort by size
# alias lc="ls -lcrh" # sort by change time
# alias lu="ls -lurh" # sort by access time
# alias lr="ls -lRh" # recursive ls
# alias lt="ls -ltrh" # sort by date
# alias lw="ls -xAh" # wide listing format
# alias ll="ls -Fls" # long listing format
# alias labc="ls -lAp" #alphabetical sort
# alias ldir="ls -l | egrep '^d'" # directories only
# alias lf="ls -l | egrep -v '^d'" # files only
# Searches for text in all files in the current folder
ftext () {
# -i case-insensitive
# -I ignore binary files
# -H causes filename to be printed
# -r recursive search
# -n causes line number to be printed
# optional: -F treat search term as a literal, not a regular expression
# optional: -l only print filenames and not the matching lines ex. grep -irl "$1" *
grep -iIHrn --color=always "$1" . | less -r
}
# Copy file with a progress bar
cpp() {
set -e
strace -q -ewrite cp -- "${1}" "${2}" 2>&1 \
| awk '{
count += $NF
if (count % 10 == 0) {
percent = count / total_size * 100
printf "%3d%% [", percent
for (i=0;i<=percent;i++)
printf "="
printf ">"
for (i=percent;i<100;i++)
printf " "
printf "]\r"
}
}
END { print "" }' total_size="$(stat -c '%s' "${1}")" count=0
}
# Copy and go to the directory
cpg () {
if [ -d "$2" ];then
cp "$1" "$2" && cd "$2"
else
cp "$1" "$2"
fi
}
# Move and go to the directory
mvg () {
if [ -d "$2" ];then
mv "$1" "$2" && cd "$2"
else
mv "$1" "$2"
fi
}
# Create and go to the directory
mkdg () {
mkdir -p "$1"
cd "$1"
}
# Goes up a specified number of directories (i.e. up 4)
up () {
local d=""
limit=$1
for ((i=1 ; i <= limit ; i++))
do
d=$d/..
done
d=$(echo $d | sed 's/^\///')
if [ -z "$d" ]; then
d=..
fi
cd $d
}
#Automatically do an ls after each cd
# cd () {
# if [ -n "$1" ]; then
# builtin cd "$@" && ls
# else
# builtin cd ~ && ls
# fi
# }
# Show the current distribution
distribution () {
local dtype
# Assume unknown
dtype="unknown"
# First test against Fedora / RHEL / CentOS / generic Redhat derivative
if [ -r /etc/rc.d/init.d/functions ]; then
source /etc/rc.d/init.d/functions
[ zz`type -t passed 2>/dev/null` == "zzfunction" ] && dtype="redhat"
# Then test against SUSE (must be after Redhat,
# I've seen rc.status on Ubuntu I think? TODO: Recheck that)
elif [ -r /etc/rc.status ]; then
source /etc/rc.status
[ zz`type -t rc_reset 2>/dev/null` == "zzfunction" ] && dtype="suse"
# Then test against Debian, Ubuntu and friends
elif [ -r /lib/lsb/init-functions ]; then
source /lib/lsb/init-functions
[ zz`type -t log_begin_msg 2>/dev/null` == "zzfunction" ] && dtype="debian"
# Then test against Gentoo
elif [ -r /etc/init.d/functions.sh ]; then
source /etc/init.d/functions.sh
[ zz`type -t ebegin 2>/dev/null` == "zzfunction" ] && dtype="gentoo"
# For Mandriva we currently just test if /etc/mandriva-release exists
# and isn't empty (TODO: Find a better way :)
elif [ -s /etc/mandriva-release ]; then
dtype="mandriva"
# For Slackware we currently just test if /etc/slackware-version exists
elif [ -s /etc/slackware-version ]; then
dtype="slackware"
fi
echo $dtype
}
# Show the current version of the operating system
ver () {
local dtype
dtype=$(distribution)
if [ $dtype == "redhat" ]; then
if [ -s /etc/redhat-release ]; then
cat /etc/redhat-release && uname -a
else
cat /etc/issue && uname -a
fi
elif [ $dtype == "suse" ]; then
cat /etc/SuSE-release
elif [ $dtype == "debian" ]; then
lsb_release -a
# sudo cat /etc/issue && sudo cat /etc/issue.net && sudo cat /etc/lsb_release && sudo cat /etc/os-release # Linux Mint option 2
elif [ $dtype == "gentoo" ]; then
cat /etc/gentoo-release
elif [ $dtype == "mandriva" ]; then
cat /etc/mandriva-release
elif [ $dtype == "slackware" ]; then
cat /etc/slackware-version
else
if [ -s /etc/issue ]; then
cat /etc/issue
else
echo "Error: Unknown distribution"
exit 1
fi
fi
}
# IP address lookup
getip () {
# External IP Lookup
echo -n "External IP: " ; curl http://ifconfig.me/ip
}