-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbash_aliases
372 lines (316 loc) · 10.7 KB
/
bash_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
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
# ~/.bash_aliases
#
# Author: Adrien Fabre
#
# This file contains useful aliases and functions
# It must be called in ~/basrc
####################################################
# local aliases #
####################################################
# Aliases used only in this machine (not synched with git)
if [ -f ~/.bash_aliases_local ]; then
. ~/.bash_aliases_local
fi
####################################################
# aliases #
####################################################
# reload .bashrc (after modifying it)
alias reloadbashrc='source ~/.bashrc'
alias reloadzshrc='source ~/.zshrc'
#ls
alias ldot='ls -d .*'
alias ll='ls -AlFh'
alias la='ls -ACF'
alias l='ls -CF'
#git
alias g='git'
alias gs='git status -s'
alias gd='improvedGitDiff'
alias gfu='improvedGitFixup'
alias gdc='git diff --cached'
alias gdw='git diff -w'
alias ga='git add'
alias gp='git push'
alias gc='git commit'
alias gl='git pull --rebase'
alias glg='git log'
alias glgs='git log --show-signature'
alias glgp='git log -p'
alias glgn='git log --name-only'
alias gco='git checkout'
alias glgme='git log --author "$(git config user.name)"'
alias glgpme='git log -p --author "$(git config user.name)"'
alias gcoi='git checkout $(git branch | fzf)'
improvedGitDiff() {
# If any argument is passed behave like git diff
if [ "$#" -gt 0 ]; then
git diff "$@"
return 0
fi
# Open fzf with currently modified files as choices
# The preview window shows the diff for the file
# $(git rev-parse --show-toplevel) returns the root of the git directory
# and {-1} is the file path from the root directory
# We need that otherwise then calling the function from a subdirectory in the project won't work
preview="git diff $@ --color=always -- $(git rev-parse --show-toplevel)/{-1}"
# bind options: Use
# ctrl-j/ctrl-k to scroll the preview
# ctrl-y to yank the current file name in the tmux buffer
bind_yank='ctrl-y:execute-silent(tmux set-buffer $(echo -n {-1}))+abort'
bind_scroll='ctrl-j:preview-down,ctrl-k:preview-up'
local selection=$( git diff "$@" --name-only | fzf -m --ansi --reverse --preview "$preview" --bind "$bind_yank" --bind "$bind_scroll" --preview-window top,90%,wrap )
# Put the selected lines in the next prompt line (only work with ZSH)
# TODO find a way to move the cursor to the begining of the line (probably with zle)
[ -n "$selection" ] && [ -n $ZSH_VERSION ] && print -z -- "${selection[@]//$'\n'/ }"
}
improvedGitFixup() {
# Make it easier to fixup a commit:
# $ git add ./whatever
# $ improvedGitFixup (or gfu) # choose the commit to update from the fzf list
# ^ this creates a new commit with some special commit message that git autosquash knows how to handle
# $ git rebase -i --autosquash origin/master # does the autosquash, which squashes the commits into the right commits
if [ "$#" -gt 1 ]; then
echo "Usage: [commit]"
echo "Expected 0 or 1 args, got $#"
return 1
fi
if $(git diff --cached --quiet); then
echo "No files have been staged. Use 'git add' before running this command."
return 1
fi
COMMIT_TO_FIXUP=$1
if [ -z $COMMIT_TO_FIXUP ]; then
preview="git log -p -n1 --color=always {1}"
bind_scroll='ctrl-j:preview-down,ctrl-k:preview-up'
COMMIT_TO_FIXUP=$(git log --color --pretty=format:'%C(red)%h%C(reset) - %s %C(green)(%cr) %C(bold blue)<%an>%C(reset)' --abbrev-commit | fzf --ansi --reverse --preview "$preview" --preview-window top,80%,wrap --bind "$bind_scroll" | cut -d' ' -f1)
fi
git commit --fixup $COMMIT_TO_FIXUP
PREVIOUS_COMMIT=$(git rev-parse --short "$COMMIT_TO_FIXUP^1")
echo '\nCreated fixup commit. Run the following command to apply:\n'
echo " git rebase -i --autosquash $PREVIOUS_COMMIT"
}
alias man='improvedMan'
improvedMan() {
# If any argument is passed behave like man
if [ "$#" -gt 0 ]; then
'man' "$@"
return 0
fi
# bind options: Use
# ctrl-j/ctrl-k to scroll the preview
# ctrl-y to yank the current file name in the tmux buffer
bind_yank='ctrl-y:execute-silent(tmux set-buffer $(echo -n {-1}))+abort'
bind_scroll='ctrl-j:preview-down,ctrl-k:preview-up'
preview="MANWIDTH=80 man {1}"
man_page=$(man -k . | fzf -m --ansi --reverse --preview "$preview" --bind "$bind_yank" --bind "$bind_scroll" --preview-window right,50%,wrap)
man_entry=$(cut -d ' ' -f1 <<< "$man_page")
man "$man_entry"
}
# if command -v fzf > /dev/null 2>&1; then
# alias gcoi='git checkout $(git branch | fzf)'
# alias gdi='git diff $(git status --porcelain | sed "s/\w //" | fzf)'
# else
# alias gcoi='echo "fzf not found no aliases"'
# alias gdi='echo "fzf not found no aliases"'
# fi
alias glgi='fzf-show-commits'
# Taken from fshow - git commit browser on https://github.com/junegunn/fzf/wiki/examples#git
fzf-show-commits() {
git log --graph --color=always \
--format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" |
fzf --ansi --no-sort --reverse --tiebreak=index --bind=ctrl-s:toggle-sort \
--bind "ctrl-m:execute:
(grep -o '[a-f0-9]\{7\}' | head -1 |
xargs -I % sh -c 'git show --color=always % | less -R') << 'FZF-EOF'
{}
FZF-EOF"
}
# Ag the silver searcher
alias agw='ag --word-regex'
# node repl
alias ni='node -i'
# npm
alias npmr='npm run'
alias pnpmr='pnpm run'
# terraform
alias tf="terraform"
alias ts='date +"%s"'
alias ts2date='timestamp2date'
function timestamp2date {
echo $(date -d @$1)
}
# Restart polybar
if [ -f ~/.config/polybar/launch.sh ]; then
alias polystart='source ~/.config/polybar/launch.sh &'
fi
# Add git completion to aliases if we are in bash
# (zsh already has that by default)
if [[ $SHELL =~ 'bash' && -f ~/.git-completion.bash ]]; then
source ~/.git-completion.bash
__git_complete g __git_main
__git_complete gs _git_status
__git_complete gd _git_diff
__git_complete ga _git_add
__git_complete gp _git_pull
__git_complete gP _git_push
__git_complete gc _git_commit
__git_complete gl _git_log
__git_complete gco _git_checkout
fi
#mkdir: create parents directories + verbose
alias mkdir='mkdir -p -v'
#power management
# shutdown
alias shutnow='sudo shutdown -h now'
# restart
alias restnow='sudo shutdown -r now'
#vim
alias v='nvim'
alias vi='vim'
alias n='nvim'
alias vim='nvim'
# directories navigation
alias back='cd $OLDPWD'
alias ..='cd ../'
alias ...='cd ../../'
alias ....='cd ../../../'
alias .....='cd ../../../../'
alias ......='cd ../../../../../'
alias .......='cd ../../../../../../'
alias ........='cd ../../../../../../../'
alias .........='cd ../../../../../../../../'
alias ..........='cd ../../../../../../../../../'
# directories navigation with z + fzf
# TODO add check for existance of the commands
alias zf='z $(z | fzf)'
# quickping with human readable timestamp
function p {
ping -vD 8.8.8.8 | while read row
do
if [[ $row == \[*\]* ]]; then
DATE=$(date +"%d-%m-%y %T" -d @$(echo $row | grep -Eo "[0-9]{10}.[0-9]{6}"))
sed -r -e "s/[0-9]{10}.[0-9]{6}/$DATE/g" -e 's/icmp.req.//g' <<< "$row"
fi
done
}
alias p1='ping 1.1.1.1'
# restore vim with a session file
alias lvim='vim -S ~/Session.vim'
# Get debian version codename
alias debianversion='lsb_release -a'
# Node
# Start node with debugging
alias noded='node --inspect-brk'
# Podman
alias pps='podman ps'
alias ppsa='podman ps -a'
# Sudo
# https://askubuntu.com/a/22043
# alias sudo='sudo '
# Use sode with the current $PATH (useful when using docker)
alias sudop='sudo env PATH=$PATH'
# Docker
# Use docker with sudo and $PATH set
alias sdocker='sudo env PATH=$PATH docker'
alias sdocker-compose='sudo env PATH=$PATH docker-compose'
alias ansible-playbook='ansiblePlaybookDebug'
function ansiblePlaybookDebug {
export ANSIBLE_STDOUT_CALLBACK=default
echo -n "$@" | grep -q -- "-v" && export ANSIBLE_STDOUT_CALLBACK=yaml
'ansible-playbook' "$@"
}
####################################################
# functions #
####################################################
# Open files or directory in GUI application
# (xdg-open is better than gnome-open because it is desktop agnostic)
function o {
# If no parameter is passed open current folder
if [ $# -eq 0 ] || [ -z $@ ]; then
xdg-open ./
else
xdg-open $@
fi
}
# function Extract for common file formats
function extract {
if [ -z "$1" ]; then
# display usage if no parameters given
echo "Usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
else
if [ -f $1 ] ; then
# creation of the folder where to extract the files
#NAME=${1%.*}
#mkdir $NAME && cd $NAME
# extraction
case $1 in
*.tar.bz2) tar xvjf ./$1 ;;
*.tar.gz) tar xvzf ./$1 ;;
*.tar.xz) tar xvJf ./$1 ;;
*.lzma) unlzma ./$1 ;;
*.bz2) bunzip2 ./$1 ;;
*.rar) unrar x -ad ./$1 ;;
*.gz) gunzip ./$1 ;;
*.tar) tar xvf ./$1 ;;
*.tbz2) tar xvjf ./$1 ;;
*.tgz) tar xvzf ./$1 ;;
*.zip) unzip ./$1 ;;
*.Z) uncompress ./$1 ;;
*.7z) 7z x ./$1 ;;
*.xz) unxz ./$1 ;;
*.exe) cabextract ./$1 ;;
*) echo "extract: '$1' - unknown archive method" ;;
esac
else
echo "$1 - file does not exist"
fi
fi
}
# Creates an archive (*.tar.gz) from given directory.
function maketar { tar cvzf "${1%%/}.tar.gz" "${1%%/}/"; }
# Create a ZIP archive of a file or folder.
function makezip { zip -r "${1%%/}.zip" "$1" ; }
# Create an archive of a given format from a given file or directory
function archive {
if [ ! -d "$2" ] && [ ! -f "$2" ];then
echo "not a file or directory"
echo "Usage: archive [format] [path/to/file/or/directory]"
return -1
fi
case "$1" in
tar.gz) tar cvzf "${2%%/}.tar.gz" "${2%%/}/";;
zip) zip -r "${2%%/}.zip" "$2" ;;
gz|gzip) cat $2 | gzip > $2.gz;;
*) echo "wrong archive type"
echo "Usage: archive [format] [path/to/file/or/directory]";;
esac
}
# looping through a command
loop() {
echo Starting: "$@"
while true; do
eval $(printf "%q " "$@")
sleep 5;
done
}
loopd() {
CMD=""
DELAY=1
re='^[0-9]+$'
# get the command and skip the first argument if it is a number
if [[ $1 =~ $re ]] ; then
DELAY=$1
CMD=$2
for i in "${@:3}"; do
CMD="$CMD $i"
done
else
CMD=$@
fi
echo Starting: "$CMD"
echo Delay: "$DELAY"
while true; do
eval $(printf "%q " "$CMD")
sleep $DELAY;
done
}