-
Notifications
You must be signed in to change notification settings - Fork 0
/
bashrc.sh
331 lines (282 loc) · 10.8 KB
/
bashrc.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
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
export EDITOR="nvim"
export DOOMDIR="~/.config/doom"
export VISUAL=$EDITOR # the variable that actually gets used for everything
# list of ~100,000 english words
export WORDLIST="/usr/share/dict/words"
# The PS1 is the string bash prints after it finishes running a command
# \H is the hostname
# \w is the full path of current working directory
# \u is the username
# PS1="\u:\H:\w$ " # Ubuntu's default
PS1="\w$ "
# enable recursive wildcard
# **/*.png will match a.png b/c.png d/e/f.png
shopt -s globstar
# alias n="$VISUAL --no-wait --alternate-editor='emacs &'"
alias n="nvim"
alias nn="nvim"
alias e="ls -t --color=auto --group-directories-first" # last modified date
alias ee="ls -talhv --group-directories-first"
alias eee="tree -a"
alias eeee="tree"
alias u="cd"
alias uu="cd ../"
alias uuu="cd ../.."
alias uuuu="cd ../../.."
alias uuuuu="cd ../../../.."
alias h="history"
alias x="dtrx" # figure out what tar or unzip command to run
alias t="trash"
alias tt="rm -rf"
# for when you actually want to delete a file
# (as long as you don't have an unencrypted ssd and aren't using a journaling file system. see `man shred`)
#alias ttt="shred -zn 3 --remove"
alias yt="youtube-dl"
alias fd=fdfind # installed as fdfind on debian based OSes
alias vim=nvim
# create a list of directories and cd into the last one.
mkd() {
# in bash, you can refer to "all of the functions arguments" as either $@ or $*
# $@ means pass each argument on its own
# $* means pass all arguments as a single argument. Wrap it in quotes to be extra safe.
mkdir -p "$@"
cd "${@: -1}" # the last argument
}
alias py="python -q -i -c 'import \
math, statistics, random, secrets, uuid, \
collections, itertools, functools, bisect, \
copy, operator, inspect, \
time, \
sys, os, traceback, subprocess, shutil, argparse, stat, hashlib, io, \
re, string, difflib, base64, \
csv, json, pickle, \
http; \
from collections import Counter, defaultdict; \
from pathlib import Path; \
from decimal import Decimal; \
from fractions import Fraction; \
from io import StringIO, BytesIO; \
import urllib; \
from urllib.parse import urlparse; \
from urllib.parse import urlunparse; \
from pprint import pprint; \
import pdb; \
from dis import dis; import ast; \
from datetime import datetime, timedelta; \
import requests; \
'"
# serve the current directory to the internet on port 8000
alias http="python -m http.server"
alias psg="pass generate -n -c" # don't use symbols in password manager
alias pss="pass show -c" # copy password to clipboard
alias psi="pass insert"
alias g="git"
# View abbreviated SHA, description, and history graph of the latest 20 commits
alias gl="git log --pretty=oneline -n 20 --graph --abbrev-commit"
# view abbreviated SHA, description, time since commit, username, and history graph of all commits
alias glg="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# View the current working tree status using the short format
alias gs="git status -s"
alias gb="git branch"
# Show the diff between the latest commit and the current state
alias gd="git diff-index --quiet HEAD -- || clear; git --no-pager diff --patch-with-stat"
# `git di $number` shows the diff between the state `$number` revisions ago and the current state
alias gdi="d() { git diff --patch-with-stat HEAD~$1; }; git diff-index --quiet HEAD -- || clear; d"
# Make a commit, showing a full diff for all changes
alias gc="git commit -v"
alias ga="git add"
# Commit all changes
alias gca="git add -A && git commit -av"
# pull changes to local checkout of fork from upstream
alias gu="git pull upstream master --rebase"
# defined in dotfiles/gitconfig
alias gp="git p" # push
alias gpl="git pl" # pull
alias gpll="git pull"
alias gcl="git clone"
alias amend="git amend" # commit --amend
alias gsu="git pull && git submodule update --recursive --remote" # pull current repo and all submodules
gau() {
echo git remote add upstream [email protected]:$1.git
git remote add upstream [email protected]:$1.git && git pull upstream
}
# Commands with options
# shuf's builtin randomness generator is insecure
alias shuf='shuf --random-source=/dev/urandom'
# make a copy of a website for offline viewing
# https://www.guyrutenberg.com/2014/05/02/make-offline-mirror-of-a-site-using-wget/
alias mirror="wget --mirror --convert-links --adjust-extension --page-requisites --no-parent"
# transfer a file over ssh and keep partial files that haven't finished transferring if the connection is cut
alias scp="rsync -P -e ssh"
# check internet connection by pinging Google's DNS server
alias internet4="ping 8.8.8.8"
alias internet6="ping 2001:4860:4860::8888"
# another option is Sprint's website
#alias internet6="ping 2600::"
alias internet=internet4
# Open urls from the commandline.
alias browser="google-chrome"
# use chrome headless as curl
alias churl="browser --headless --dump-dom"
# Copy tmux buffer into system clipboard
alias tmcp="tmux show-buffer | pbcopy"
# make a file executable
alias chmox="chmod +x"
# open files with gui apps on Linux
alias open=xdg-open
# lowercase and uppercase stdin
alias lower="tr '[:upper:]' '[:lower:]'"
alias upper="tr '[:lower:]' '[:upper:]'"
# alert for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
# delete useless python interpreter artifacts
# TODO: I think the .py[co] part might not be necessary, and all those files are in __pycache__
pyclean () {
find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__ -delete
}
# Updating
pipall() { # all pip packages. https://github.com/pypa/pip/issues/59
pip3 install --upgrade pip
pip3 list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip3 install -U
}
alias upgrade=update
alias reinstall_doom="rm -rf ~/.emacs.d/ && git clone --depth 1 https://github.com/hlissner/doom-emacs ~/.emacs.d && ~/.emacs.d/bin/doom install --env --fonts"
# Generate new ssh key as recommended by https://blog.g3rt.nl/upgrade-your-ssh-keys.html
# the `-C ''` prevents storing hostname with the ssh key
alias new-ssh-key="ssh-keygen -o -a 100 -t ed25519 -C ''"
# print main ssh key
alias ssh-key="cat ~/.ssh/id_ed25519.pub"
# if you need need a quick source of url-safe random data
alias random-hash="head -c 1024 /dev/urandom | shasum -a 256"
alias random-base32="head -c 30 /dev/urandom | base32"
alias random-base64="head -c 30 /dev/urandom | base64"
alias random-number="shuf --random-source=/dev/urandom -i 1-1000000000000000000 -n 1"
alias random-letters="cat /dev/urandom | tr -dc 'a-z' | fold -w 32 | head -n 1"
# correct battery horse staple
alias random-password="curl -s https://raw.githubusercontent.com/first20hours/google-10000-english/master/google-10000-english-no-swears.txt | shuf --random-source=/dev/urandom | head -n 4 | tr '\n' ' '; echo"
# press ctrl-d when you want to stop timing
alias stopwatch="echo press Ctrl-c to stop the timer.; TIMEFORMAT=%R; time cat; unset TIMEFORMAT"
alias hideprompt="export PS1='$ '"
alias hideps1=hideprompt
random() {
if [ "$#" -eq 0 ]; then
shuf -i 0-10000 -n 1 --random-source=/dev/urandom
fi
if [ "$#" -eq 1 ]; then
shuf -i 0-$1 -n 1 --random-source=/dev/urandom
fi
if [ "$#" -eq 2 ]; then
shuf -i $1-$2 -n 1 --random-source=/dev/urandom
fi
}
# Rg interprets the second argument as the direcotory to search in,
# usually my query just has a space in it
rgg() {
rg "$*"
}
# is a string a top level domain?
alias tlds="curl -s https://data.iana.org/TLD/tlds-alpha-by-domain.txt | lower"
is_tld() {
if [[ "$#" -eq 0 ]]; then
tlds
else
tlds | rg -i "$1"
fi
}
# with no arguments 'o' opens the current directory,
# otherwise opens the given location
o() {
if [[ "$#" -eq 0 ]]; then
xdg-open .
else
xdg-open "$1"
fi
}
# finds a process and kills it
find_kill() {
kill `ps aux | grep $@ | awk '{print $2}'`
}
pw() { # get full path to current directory or to a specified file in current directory
if [[ "$#" -eq 0 ]]; then
pwd
else
readlink -m "$@"
fi
}
export GOPATH=~/go
# use go binaries
PATH=$PATH:$GOPATH/bin
# use rust binaries
PATH=$PATH:~/.cargo/bin
# use doom emacs commands
PATH=$PATH:~/.emacs.d/bin
PATH=$PATH:/snap/bin
# store a list of all the commands I've every issued in ~/.bash_eternal_history
# https://stackoverflow.com/questions/9457233/unlimited-bash-history
export HISTFILESIZE=
export HISTSIZE=
export HISTTIMEFORMAT="[%F %T] "
# Change the file location because certain bash sessions truncate .bash_history file upon close.
# https://superuser.com/questions/575479/bash-history-truncated-to-500-lines-on-each-login
export HISTFILE=~/.bash_eternal_history
# Force prompt to write history after every command.
# https://superuser.com/questions/20900/bash-history-loss
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
# stop collecting bash history in the current terminal
# useful for copy pasting sensitive data
alias forget="unset HISTFILE"
#export HISTCONTROL=ignoreboth
export RIPGREP_CONFIG_PATH=~/.config/ripgrep.conf
journal() {
birthday=19960201 # YYYYMMDD
days_since_birth=$(echo "( `date +%s` - `date -d $birthday +%s`) / (24*3600)" | bc)
# don't get the date twice, in case I call it at midnight on the last day of the year
current_time=$(date)
year=$(date --date="$current_time" +%Y)
day_zero_indexed=$(printf "%0*d\n" 3 $((10#$(date --date="$current_time" +%j)-1)))
mkdir -p ~/Documents/journal/$year
$VISUAL ~/Documents/journal/$year/$day_zero_indexed
}
alias j="journal"
# Switch between dvorak (default), russian and qwerty.
alias keyboard_options='setxkbmap -option caps:escape -option grp:shifts_toggle;\
xset r rate 180 30; xmodmap -e "keycode 9 = dollar asciitilde"'
alias aoeu="setxkbmap -layout us,ru,us -variant dvp,,; keyboard_options"
alias asdf="aoeu"
alias yayc='yay -Sc' # clean pacman
alias yayo='yay -Qtdq' # orphaned packages
alias reboot='sudo systemctl reboot'
alias shutdown='sudo systemctl poweroff'
function _os {
case $OSTYPE in
linux*) if [[ -f /etc/arch-release ]]; then echo arch
elif [[ -f /etc/debian_version ]]; then echo debian
fi ;;
darwin*) echo macos ;;
esac
}
function _is_callable {
for cmd in "$@"; do
command -v "$cmd" >/dev/null || return 1
done
}
# Clipboard pipes
if _is_callable xclip; then
alias y='xclip -selection clipboard -in'
alias p='xclip -selection clipboard -out'
elif _is_callable xsel; then
alias y='xsel -i --clipboard'
alias p='xsel -o --clipboard'
elif _is_callable pbcopy; then
alias y='pbcopy'
alias p='pbpaste'
fi
# macOS specific aliases
if [[ $(uname) == "Darwin" ]] && [[ -f ~/.bash_mac ]]; then
. ~/.bash_mac
fi
# local config file not tracked by git
if [[ -f ~/.bash_local ]]; then
. ~/.bash_local
fi