forked from farzadghanei/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.bash_aliases
More file actions
282 lines (249 loc) · 8.31 KB
/
.bash_aliases
File metadata and controls
282 lines (249 loc) · 8.31 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
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='grep -E --color=auto'
fi
if $(command -v nvim &> /dev/null) || [ -e $HOME/.local/bin/nvim ]; then
export EDITOR=nvim
alias vim=nvim
fi
if $(command -v tmux &> /dev/null) || [ -e $HOME/.local/bin/tmux ]; then
if [ -z "$TMUX" ]; then
# DISABLED 20250403 because it breaks python venv + scrolling + alt-.
#tmux attach || $HOME/scripts/tmux_new_session.sh
fi
fi
# Show all services listening to ports
alias lsofp='lsof -Pnl +M -i4'
# some more ls aliases
alias ll='ls -hlAF'
# mkdir goes to created dir
function mkd {
mkdir "$1"
cd "$1"
}
alias lln="ls -lhtr --time-style long-iso | tac | cat -n | tac | sed -s 's/^\s*\([0-9]*\)\s*\(.*\)/[\1] \2 [\1]/'g && pwd"
function lf {
if [ $# -eq 0 ];
then
local n=1
else
local n="${1}"
fi
ls -rt1 | tail -n ${n} | head -n 1
}
function expand_dirs {
# Expand ... to ../..
function vbe-expand-dot-to-parent-directory-path() {
case $LBUFFER in
(./..|* ./..) LBUFFER+='.' ;; # In Go: "go list ./..."
(..|*[ /=]..) LBUFFER+='/..' ;;
(*) LBUFFER+='.' ;;
esac
}
zle -N vbe-expand-dot-to-parent-directory-path
bindkey "." vbe-expand-dot-to-parent-directory-path
bindkey -M isearch "." self-insert
}
expand_dirs
function log() {
git log --graph --color=always \
--format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" |
fzf --ansi --no-sort --reverse --tiebreak=index --toggle-sort=\` \
--bind "ctrl-m:execute:
echo '{}' | grep -o '[a-f0-9]\{7\}' | head -1 |
xargs -I % sh -c 'git show --color=always % | less -R'"
}
# Grep from $1 until $2 in $3+
function gu() {
sed -n -e "/$1/,/$2/ p" ${@:3}
}
# git aliases
alias ga='git add'
alias gas="$HOME/scripts/git-stage-staged-files.sh"
alias gst='git status'
alias gpf='git push -f'
alias gsp='git stash pop'
alias gcm="$HOME/scripts/git_checkout_master.sh"
alias gp='git pull'
alias gc='git commit -v'
alias gca='git commit --amend -v'
alias gdf='git diff'
alias gch='git checkout'
alias gpb='git checkout -'
alias grh='git reset --hard'
alias gph='git push -u origin HEAD'
alias doit='ga .; gca --no-edit; gpf'
alias gitparent='git show-branch |& grep "*" | grep -v "$(git rev-parse --abbrev-ref HEAD)" | head -n1 | sed "s/.*\[\(.*\)\].*/\1/" | sed "s/[\^~].*//"'
function gpr {
local tmpfile=$(mktemp)
trap "rm $tmpfile" EXIT
local initial_title=$(git log --pretty=format:%s HEAD~1..HEAD)
local initial_body=$(git log --pretty=format:%b HEAD~1..HEAD | tr '\n' ' ' | sed 's/^ //g')
echo $initial_title > $tmpfile
echo "" >> $tmpfile
echo $initial_body >> $tmpfile
$EDITOR $tmpfile
local title=$(cat $tmpfile | head -n1)
local body=$(cat $tmpfile | tail -n+3)
local url=$(gh pr create --title "$title" --body "$body" | tail -n1)
echo "$url $title"
}
function rmcommit {
# Get all commits up to the given commit
commits=`git log $1..HEAD --pretty=format:%H`
echo "Stashing changes"
git stash
echo "\nSetting HEAD to just before commit $1"
git reset --hard $1~1
echo "\nApplying inbetween commits: $commits"
echo $commits | xargs git cherry-pick
echo "\nPopping stash"
git stash pop
}
# Kubernetes aliases
alias k='kubectl'
alias kubeload='kubectl get node -o json | jq -r ".items[].metadata.name" | xargs -I {} ssh {} "hostname; w; echo"'
# docker aliases
alias drmc='docker rm $(docker ps -qa --no-trunc --filter "status=exited")'
alias drmi='docker rmi $(docker images -a --filter=dangling=true -q)'
alias drmv='docker volume ls -qf dangling=true | xargs -r docker volume rm'
alias dc='docker-compose'
function dcip {
local DC_NAME=$1
local DC_PROJECT=$(basename $(pwd))
dc ps -q $DC_NAME | xargs docker inspect | jq -r ".[0].NetworkSettings.Networks.${DC_PROJECT}_default.IPAddress"
}
function katt {
dc kill $1
dc rm -f $1
dc up --force-recreate -d $1
}
# Web development
alias wpb="./node_modules/.bin/webpack --progress --config webpack.config.js --colors"
# Commonly used repos
alias dotf="cd $HOME/code/dotfiles"
# Systemctl aliases
alias susy='sudo systemctl'
if [[ `command -v complete` ]]; then complete -F _complete_alias susy; fi
# Pip stuff
alias pir='pip install -r requirements/development.txt'
alias s='ssh -o stricthostkeychecking=no -o userknownhostsfile=/dev/null'
function jh {
if ping -c1 -W1 alpha 2&>1 /dev/null; then
ssh -D 5000 jh -J alpha
else
ssh -D 5000 jh
fi
}
# Venv stuff
alias de="cd ~; deactivate"
export DISTROBOX_DEFAULT_NAME=debian-buster
function dbe {
if [ $DISTROBOX_ENTER_PATH ]; then
echo "Distrobox is already active"
exit 1
fi
local db_name=${1:-$DISTROBOX_NAME}
if [ -n "$db_name" ]; then
echo "Entering distrobox $db_name"
distrobox enter --name $db_name
else
echo "Assuming default distrobox $DISTROBOX_DEFAULT_NAME"
distrobox enter --name $DISTROBOX_DEFAULT_NAME
fi
}
function dbb {
if [ $DISTROBOX_ENTER_PATH ]; then
echo "Distrobox is already active"
exit 1
fi
local name=${DISTROBOX_NAME:-$DISTROBOX_DEFAULT_NAME}
echo "Making $name in $PWD"
if [ -f .distrobox/.env ]; then
docker build $(eval $(cat .distrobox/.env | xargs echo eval echo) | xargs -n1 echo --build-arg) -t distrobox-$name $1 .distrobox/
else
docker build -t distrobox-$name $1 .distrobox/
fi
distrobox list | grep -q $name && distrobox rm $name -f
distrobox create --image distrobox-$name $name
}
# Create venv in current dir
function mkv {
if [[ `command -v poetry` ]]; then
if [ -f poetry.lock ]; then
poetry install
return
fi
fi
local venv_name=$(basename $(pwd))
if [ -z $VIRTUAL_ENV ]; then
mkvirtualenv -a . -p python3 $venv_name $1
else
echo "Virtualenv $(basename $VIRTUAL_ENV) already active, not making new one"
fi
if [ -f requirements.txt ]; then
pip install -r requirements.txt
elif [ -f requirements/development3.txt ]; then
pip install -r requirements/development3.txt
elif [ -f requirements/development.txt ]; then
pip install -r requirements/development.txt
fi
if [[ `command -v pre-commit` ]]; then
pre-commit install --allow-missing-config
fi
}
alias rt='$(cat $VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME)/runtests.sh -1'
# On Buster, fd is installed under fdfind
if [ -x /usr/bin/fdfind ]; then
alias fd='fdfind'
fi
# Utilities
alias sum='python -c "import sys; print(sum(int(l) for l in sys.stdin))"'
alias chmox="chmod +x"
function check_cert {
local DOMAIN=$1
openssl s_client -connect $DOMAIN:443 -servername $DOMAIN < /dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates
}
alias check_domain=check_cert
# Tasks
alias t='task'
alias tw='timew'
# Nomad
function get_nomad_alloc_id {
nomad job status -verbose $1 | grep $1 | grep running | cut -d' ' -f1
}
function nl {
get_nomad_alloc_id $1 | xargs -P 2 -I {} nomad alloc logs "${@:2}" {}
}
# yay browser
function yay-ls () {
yay -Slq | fzf -m --preview 'cat <(yay -Si {1}) <(yay -Fl {1} | awk "{print \$2}")' | xargs -ro yay -S
}
# Activate venv based on directory name
function wo {
workon $(basename $PWD)
}
alias start_all_distroboxes="distrobox list | awk '{print\$3}' | grep -v NAME | xargs -n1 distrobox enter"
alias stop_all_distroboxes="distrobox list | grep Up | awk '{print\$3}' | xargs -n1 distrobox stop -Y"
alias mutt=neomutt
if [[ $(command -v gem) ]]; then
export GEM_HOME="$(ruby -e 'puts Gem.user_dir')"
export PATH="$PATH:$GEM_HOME/bin"
fi
alias sus='sort | uniq -c | sort -n'
function compare_ns {
echo "nsa.byte.nl: $(dig @nsa.byte.nl $1 +short +all)"
echo "ns1.hypernode.com: $(dig @ns1.hypernode.com $1 +short +all)"
}
alias hm="home-manager"
alias hms='nix run home-manager/master -- switch --flake ".#$(hostname)"'
function kns {
kubectl config set-context --current --namespace="${1}"
}
for f in $(find $HOME/.bash.d -type f); do source $f; done