-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.sh
More file actions
408 lines (365 loc) · 15.6 KB
/
shell.sh
File metadata and controls
408 lines (365 loc) · 15.6 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/bin/bash
#
# Zsh/bash aliases, exports, and functions.
#
# One thing to remember is that the aliases only work if they are the first part
# of a command. If you want to do something like `ls | dmenu` or `ls | xargs vim`,
# then aliases for `dmenu` and `vim` will be ignored. So to make such use cases work,
# instead of aliases some programs have thin wrapper executables under `dotfiles/bin`.
# Coreutils and their replacements.
if type eza > /dev/null 2>&1; then
# These might be available in future versions (currently in git master):
# --no-permissions --no-filesize --no-user --no-time
alias la='eza -la --group-directories-first'
alias lat='eza -la -snew --group-directories-first'
alias lar='eza -laR --group-directories-first'
alias lag='eza -la --group-directories-first | \rg -i'
elif type exa > /dev/null 2>&1; then
alias la='exa -la --group-directories-first'
alias lat='exa -la -snew --group-directories-first'
alias lar='exa -laR --group-directories-first'
alias lag='exa -la --group-directories-first | \rg -i'
else
alias ls='\ls --color=auto --group-directories-first'
alias la='\ls -lahF --color=auto --group-directories-first'
alias lat='\ls -lahFtr --color=auto --group-directories-first' # sort by time (reverse)
alias lar='\ls -lahFR --color=auto --group-directories-first' # recursive
alias lag='\ls -lahF --color=auto --group-directories-first | \rg -i'
fi
alias cp='\cp -i' # prompt before overwriting
alias mv='\mv -i' # prompt before overwriting
alias chx='chmod +x' # make file executable
alias fs='du -shD' # size of file or folder. s:summarize, h:human_readable, D=dereference_links
alias df='df -hT' # Show file system space usage in human readabale format.
alias lns='ln -s' # Create symbolic link
# Searching. "rg" is a "grep" replacement
# -i: ignore case
# --hidden: include hidden files in search
alias rg='\rg -i --hidden'
alias rgi='\rg --hidden'
alias rgl='\rg -li' # Print filenames of matches instead.
# I always forget how to search only in files of specific name, it's: rg <search> -g <filename>
# where `-g` is short for `--glob`. See also `bin/rglg` which adds `-l` to only list the file names.
# "fd" is a "find" replacement. The binary name is different in some distros.
if type fdfind > /dev/null 2>&1; then
alias lf='\fdfind -H -I -d 1 -X du -shx | sort -h'
else
alias lf='\fd -H -I -d 1 -X du -shx | sort -h' # find large directories/files in current folder. Btw, if /var is filling /, then you probably forgot to run `pacman -Sc` for a year.
fi
# Misc
alias en='LANG=en_EN.UTF-8' # Prefix commands with this to use the English locale.
alias ssha='ssh-add -t 150000 $(find ~/.ssh | grep id | grep -v pub)'
alias info='\info --vi-keys'
alias uusb='devmon -u' # unmount everything mounted by devmon (included in udevil)
alias gnutime="/usr/bin/time -f 'user: %U, sys: %S, percentage: %P, wall %e'"
# `gr myprogram --myargs` will run until crash, print backtrace and exit. There is also `--batch` option but it does not print in color(?).
alias gr='gdb -q --ex run --ex bt -ex="set confirm off" --iex "set pagination off" --ex quit --args'
alias packets='ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=nb_read_packets -of csv=p=0'
# Media
alias za='zathura'
alias sxiv='echo "use im instead"'
alias feh='echo "use im instead"'
alias mp='\ncmpcpp' # [m]usic [p]layer
alias mpv_fix_mono='mpv --audio-channels=1'
alias mpv2='\mpv --vo=vdpau' # This fixed some video playback issue when using the default --vo=gpu.
alias jj='cd "$(fd . -t d -d 4 | fzf)"'
for i in $(seq 1 4); do
alias ims$i="find . -maxdepth $i | grep -iE \"gif|png|jpg|jpeg|webp|avif|heic\" | sort | im -i"
alias vids$i="find . -maxdepth $i | grep -iE \"gif|png|jpg|jpeg|mkv|mp4|avi|mov|webm\" | sort | mpv --playlist=-"
alias j$i='cd "$(fd . -t d -d $i | fzf)"'
done
alias ims='ims1'
alias vids='vids1'
# Git
# Git has its own alias functionality but I don't want to type the "git " prefix to use it.
alias add='git add'
alias branch='git branch -v'
# TODO Start use the new `switch` and `restore` commands instead of `checkout`:
# <https://www.banterly.net/2021/07/31/new-in-git-switch-and-restore/>
alias ch='git checkout'
alias chb='git checkout -b'
# alias cs='git switch' # Same as eg `git checkout old-branch`
# alias csc='git switch -c' # Same as eg `git checkout -b new-branch`
# alias cre='git restore` # Same as eg `git checkout -- file`. I think `restore` doesn't need `--`.
alias cherry-pick='git cherry-pick'
alias clone='git clone'
alias commit='git commit'
alias fe='git fetch --tags --prune'
# Setting $GIT_PAGER like that seems to fix an issue where few lines at top of the log are "invisible" until I scroll down and up (or press g).
alias log='GIT_PAGER="less" git log --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" --abbrev-commit'
# Same but shows date of each commit instead of age.
alias logdate='GIT_PAGER="less" git log --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset" --abbrev-commit --date=short'
alias merge='git merge'
alias mergeff='git merge --ff'
alias pull='git pull'
alias push='git push'
alias pushh='git push --set-upstream origin $(git branch --show-current)'
alias rebase='git rebase -i'
alias rc='git rebase --continue'
alias rom='git rebase -i origin/main'
alias romaster='git rebase -i origin/master'
alias remote='git remote'
alias revert='git revert'
alias show='git show'
alias sta='git diff --stat HEAD~1 | head -n -1' # Summary of file line changes for corresponding `show`.
alias recentf="git diff --stat HEAD~1 | head -n -1" # Same as `sta`, but see the variants below.
# show0 == show, sta0 == sta, recentf0 == recentf
for i in $(seq 0 9); do
alias show$i="git show HEAD~$i"
# Files changed in the last i:th commit.
alias sta$i="git diff --stat HEAD~$(($i + 1)) HEAD~$i | head -n -1"
# Files changed in the last i commits together.
alias recentf$i="git diff --stat HEAD~$(($i + 1)) HEAD | head -n -1"
done
alias stash='git stash'
alias status='git status'
alias s='git status -sb'
alias tag='git tag'
alias sm='git submodule'
alias sma='git submodule add'
alias sms='git submodule sync'
alias smu='git submodule update'
alias smuir='git submodule update --init --recursive'
# To remove a submodule see `smr` function below.
alias reflog='git reflog'
# -v: verbose, shows the diff in the commit message editor. This can also be set as git config option.
alias c='git commit -v'
alias ca='git commit -v -a'
alias cam='git commit -v --amend'
alias d='in-git-repository && git diff'
alias dc='in-git-repository && git diff --cached'
# Like `git diff` but for untracked files.
function dt() {
for file in $(git ls-files --others --exclude-standard); do
git --no-pager diff --no-index /dev/null $file
done
}
# Grab all unstaged and stanged changes and commit them with the message "e" or "v".
function ce() {
cd "$(git rev-parse --show-toplevel)"
git add .
git commit -a -m "e"
}
function cv() {
cd "$(git rev-parse --show-toplevel)"
git add .
git commit -a -m "v"
}
function smr() {
git submodule deinit -f -- "$1" &&
git rm -f -- "$1"
# And then commit the changes.
}
# List branches recently committed to.
alias recent='git branch -a --sort=-committerdate | grep -v remotes/ | head -n 20'
alias recent-remote='git branch -a --sort=-committerdate | grep remotes/ | head -n 20'
# alias recent-alt='git for-each-ref --sort=-committerdate refs/heads/ | head -n 20'
# `last-edited path/to/file` shows latest changes applied to the given file.
alias last-edited='git log -p --'
# Change git repository HTTPS links to SSH.
# Use when you want to push to your own public repositories (cloned with HTTPS or using such submodule addresses).
alias gitssh='git config url."git@github.com:".insteadOf "https://github.com/"'
## Pacman
# Use the English locale in case of something goes wrong. Capital first letter indicates sudo use.
alias PSyu='sudo LANG=C pacman -Syu' # The usual update all command.
alias PSyy='sudo LANG=C pacman -Syy' # Just sync. DO NOT run this before -S without -Syu because it may cause "partial update", see the arch wiki.
alias PS='sudo LANG=C pacman -S' # Install specific package(s).
alias PUx='sudo LANG=C pacman -U *pkg.tar.xz' # Install a just-built AUR package in the pwd.
alias PUz='sudo LANG=C pacman -U *pkg.tar.zst' # Install a just-built AUR package in the pwd.
alias PR='sudo LANG=C pacman -R' # Remove the specified package(s), retaining its configuration(s) and required dependencies
alias PRs='sudo LANG=C pacman -Rs' # Also remove dependencies of the packages, but not recursively so.
alias PSc="sudo LANG=C pacman -Sc" # Clean cache - delete all not currently installed package files.
alias Pclean="sudo LANG=C pacman -Sc" # Clean cache - delete all not currently installed package files.
alias PSkeys="sudo LANG=C pacman -Sy archlinux-keyring" # Usually this fixes PGP issues from updating packages.
alias pQ='LANG=C pacman -Q' # List all installed packages.
alias pfiles="LANG=C pacman -Ql" # List all files installed by a given package.
alias porphan="LANG=C pacman -Qdtq" # List all packages which are orphaned.
## Debian family package management. The names match the pacman aliases above.
alias AS='sudo apt-get install'
alias AR='sudo apt-get remove'
alias DS='sudo dpkg -i' # Install `.deb` file.
alias aQ='apt list --installed' # List all installed packages.
## Rust
alias cb='cargo build'
# --watch: Watch git root instead of $PWD because of my project crate organization.
alias cww='cargo watch --clear --watch "$(git rev-parse --show-toplevel)"'
alias cr='cargo run --'
# --tests: Do not run doc tests etc.
# --nocapture: Show prints from tests.
alias ct='cargo test --tests -- --nocapture'
alias cn='cargo nextest run' # --nocapture makes too verbose
alias cbr='cargo build --release'
alias crr='cargo run --release --'
# `cargo install cargo-deps`. `dot` is in the graphviz package.
alias depgraph='cargo deps --all-deps | dot -Tpng > graph.png'
alias depgraph1='cargo deps --all-deps --depth 1 | dot -Tpng > graph.png'
alias depgraph2='cargo deps --all-deps --depth 2 | dot -Tpng > graph.png'
# <https://github.com/kbknapp/cargo-outdated>
alias outdated='cargo outdated -R'
# Docker
alias dk='docker'
alias dkc='docker container'
alias dkls='docker container ls --all'
alias dkps='docker ps -a'
alias dki='docker image'
alias dkr='docker run'
alias dkri='docker run -it' # interactive, tty
alias calc='calc -d'
alias w='$HOME/dev/other/wall/target/debug/wall'
# Not exactly to my taste, but I use GTK so rarely I didn't investigate options.
export GTK_THEME=Adwaita:dark
# Common linux environment variables
export CC=/usr/bin/clang
export CXX=/usr/bin/clang++
# There is the `dotfiles/bin/vim` executable which runs neovim if it's installed.
export EDITOR="vim"
export VISUAL="vim"
export PAGER="less"
export BROWSER="firefox"
# zsh and bash history size in lines.
export HISTSIZE=10000
export SAVEHIST=10000
# `less` arguments
# -i: ignore case is search
# -n: no line numbers
# -z: Scrolling behavior
# -g: hilight only current search match
# -M: ? --LONG-PROMPT
# -X: ? Supplying this makes the output not clear from the screen, which I didn't like.
# -F: Exit automatically if file fits the screen. Didn't like.
# -R: ? tl;dr
# -j: ? Line placement.
export LESS='-i -n -g -M -R -j0'
export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;31m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
#export LESS_TERMCAP_so=$'\E[01;44;33m'
export LESS_TERMCAP_so=$'\E[01;31;31m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;32m'
# “Note that an empty entry is interpreted as the current directory, because that's how POSIX
# defined PATH, and POSIX hates you. This means that these variables must not begin or end
# with :, and must not contain :: anywhere in the middle.”
# LD_LIBRARY_PATH=$(echo $LD_LIBRARY_PATH | sed -E -e 's/^:*//' -e 's/:*$//' -e 's/:+/:/g')
# LIBRARY_PATH=$(echo $LIBRARY_PATH | sed -E -e 's/^:*//' -e 's/:*$//' -e 's/:+/:/g')
# Rust
# This directory is used by upstream rustup (ie when installing with curl script).
if [ -d "$HOME/.cargo" ]; then
export PATH="$PATH:$HOME/.cargo/bin"
fi
export RUST_BACKTRACE=1
# This finds the sources installed by `rustup component add rust-src`.
# It's handy because running the rustup update keeps compiler and sources
# versions in sync.
type rustc >/dev/null 2>&1 && export RUST_SRC_PATH=$(rustc --print sysroot)/lib/rustlib/src/rust/src/
export VDPAU_NVIDIA_NO_OVERLAY=1
## My own variables
# The command by which prefered terminal is launched
export TERM_CMD="alacritty"
#export FONT_PANGO="DejaVu Sans Mono 12"
export FONT_PANGO="DejaVu Sans Mono for Powerline 12"
# User folder that stores information from scripts
export STATE_FOLDER="$HOME/.state"
# `cd` without manual tab-completing. Also an easier to type alias, as 'cd'
# uses the same finger in the standard touch typing style.
function k() {
# Use `cd` for the full parts.
dir="$(dirname $1)"
cd "$dir"
# Complete the last part.
base="$(basename $1)"
matches="$(ls -d */ | grep -iF "$base")"
count=$(echo $matches | wc -l)
if (( $count == 0 )); then
exit
elif (( $count > 1 )); then
# For multiple matches, pick the shortest and print all.
matches=$(echo "$matches" | perl -e 'print sort { length($a) <=> length($b) } <>')
echo "$matches"
cd "$(echo "$matches" | head -n 1)"
else
cd "$matches"
fi
}
# Compact `watch git diff`.
function wgd() {
if [[ $(git rev-parse --is-inside-work-tree) = true ]]; then
# Use grep to remove three less informative lines. Might be some flags in git-diff
# to do this better. Now it matches any line containing "index ". I couldn't get
# beginning of line `^` to work, possibly because of color escape sequences.
watch -c "git diff -U1 --color=always | grep -E -v '\-\-\-|\+\+\+|index '"
fi
}
# `cd` into path under current git project root. Outside git use $PWD.
function cdg() {
cd "$(git rev-parse --show-toplevel 2> /dev/null || echo ".")/$@"
}
# Change (all) staged content to non-staged changes.
function rs() {
if [ $# -ge 1 ]; then
git restore --staged "$1"
else
git restore --staged "$(git rev-parse --show-toplevel)"
fi
}
# Stream first youtube search result.
function yv() {
youtube-dl \
--default-search=ytsearch: \
--youtube-skip-dash-manifest \
-o - \
"$*" \
| mpv -
}
# Audio only.
function ya() {
youtube-dl \
--default-search=ytsearch: \
--youtube-skip-dash-manifest \
-o - \
--format="bestaudio[ext!=webm]" \
"$*" \
| mpv -
}
# Add all search hits from the music library and play in order.
function play() {
mpc clear
mpc search title "$*" | mpc add
mpc play
}
# Paranoid about making typos, avoid typing `rm -rf` to remove a git repository.
function rmgit() {
if [ $# -ne 1 ]; then
echo "Usage: gitrm <dir>"
return
fi
if [ -d "$1/.git" ]; then
rm -rf "$1/.git" && rm -r "$1"
else
echo "Not a git repository."
fi
}
# Modern kids might instead call this "YOLO".
# Don't use this ever. Except when you really need to. Alright go ahead, I'm not responsible.
function roll() {
cd "$(git rev-parse --show-toplevel)"
git add .
git commit --amend --no-edit
git push --force
}
# When you start a GUI program from shell you often get a spammy terminal which you can't close
# because it would also close the GUI program. Press on such terminal "Ctrl-Z" and then run this
# in it to make the GUI program independent and get rid of terminal window. (Tip: `fg` reverts Ctrl-Z)
diso() {
bg %1 && disown %1 && exit
}
# Try prefix command with this if the UI scale is too small.
hidpi() {
GDK_SCALE=2 \
QT_SCALE_FACTOR=2 \
GDK_DPI_SCALE=1 \
QT_AUTO_SCREEN_SCALE_FACTOR=0 \
QT_ENABLE_HIGHDPI_SCALING=1 \
"$@"
}