-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbash.sh
245 lines (204 loc) · 6.58 KB
/
bash.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
# Install the following tools
# rust: ripgrep, git-delta, eza, bat, cargo-update,
# other: git, wget, tmux, zathura, neovim, xclip, fzf, bfs
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
# Replace capslock with additional CTRL
setxkbmap -option caps:ctrl_modifier,shift:both_capslock
function switch-net() {
wpa_cli list_networks
read -n 1 -p "Please select a network: " NETWORK
wpa_cli select_network $NETWORK
}
# Set PATH
PATH="$PATH:$HOME/.local/bin"
# Always cd into folders in home folder, no matter where we are
CDPATH=:~
# Do not store commands starting with space in bash history
HISTCONTROL="ignorespace"
#Some terminal setting to make signing commits work
GPG_TTY=$(tty)
export GPG_TTY
# Set nvim as our editor
if [[ -x "$(command -v nvim)" ]]; then
export EDITOR=/usr/bin/nvim
alias n='nvim'
elif [[ -x "$(command -v vim)" ]]; then
export EDITOR=/usr/bin/vim
alias n='vim'
fi
# Set visual editor like editor
export VISUAL=$EDITOR
# Clear terminal history
alias delhistory='cat /dev/null > ~/.bash_history && history -c'
# Update rust
if [[ -d "$HOME/.cargo" ]]; then
. "$HOME/.cargo/env"
alias rsup='rustup update && cargo install-update --all'
alias c='cargo'
alias cc='c c'
alias ct='c t'
fi
# Some frequent shortcuts
if [[ -x "$(command -v xdg-open)" ]]; then
alias open='xdg-open'
fi
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias o='open'
alias ss='. ~/.bashrc'
# Make our google searches a little bit faster
function g() {
open "https://google.com/search?q="$(printf "+%s" "$@")
}
if [[ -x "$(command -v eza)" ]]; then
alias ls='eza --icons'
alias ll='eza -algF --icons'
function tree() {
if [[ "$#" -eq 1 ]]; then
eza -lgF --tree --git-ignore --icons -L $1
else
eza -lgF --tree --git-ignore --icons
fi
}
fi
if [[ -x "$(command -v git)" ]]; then
alias gs='git status -sb'
alias ga='git add -A'
alias gd='git diff HEAD'
alias gl='git log'
alias gitgraph="git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - "`
`"%C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(auto)%d%C(reset)'"
fi
# Simple variant of locate
# shows path of a file
function where() {
find . -iname "*$1*"
}
# We can query the internet for awesome terminal command help
function cheat() {
curl cht.sh/$1
}
# Lookup weather in city ...
function weather() {
curl wttr.in/$1
}
# Create private keys
function random_pk {
cat /dev/urandom | tr -dc 'a-f0-9' | fold -w 64 | head -n $1
}
# Recursively replace in all files, but ignore folders starting with dot like .git
function rerep() {
find . \( ! -regex '.*/\..*' \) -type f | xargs sed -i 's/'"$1"'/'"$2"'/g'
}
# VPN stuff
if [[ -x "$(command -v wg)" && -x "$(command -v wg-quick)" ]]; then
function vpn() {
if [ "$1" = "u" ]; then
wg-quick up "$2"
if [[ -e "$HOME/._proxyconf.pac" ]]; then
mv "$HOME/._proxyconf.pac" "$HOME/.proxyconf.pac"
fi
elif [ "$1" = "d" ]; then
local current=$(sudo wg show | awk 'FNR == 1 { print $2}')
if [[ ! -z "$current" ]]; then
wg-quick down ${current};
fi
if [[ -e "$HOME/.proxyconf.pac" ]]; then
mv "$HOME/.proxyconf.pac" "$HOME/._proxyconf.pac"
fi
else
echo "Usage: vpn [u|d] [interface]"
fi
}
fi
# Copy whole file, or lines A:B from a file to clipboard
if [[ -x "$(command -v bat)" && -x "$(command -v xclip)" ]]; then
function lc() {
if [[ "$#" -eq 1 ]]; then
bat -pp $1 | xclip -se c
else
bat -pp -r $1 $2 | xclip -se c
fi
}
fi
# Diff a remote file against a local file
if [[ -x "$(command -v delta)" && -x "$(command -v wget)" ]]; then
function rdiff() {
mkdir -p /tmp/cmpr && cd /tmp/cmpr
tmpfilename=$(sed 's:.*/::' <<< $1)
wget -qO $tmpfilename $1 && cd - >/dev/null
delta /tmp/cmpr/${tmpfilename} $2
}
fi
# Count lines of code in a directory recursively for all files
# having a file ending
function loc() {
find . -name "*.$1" | xargs wc -l | sort -nr
}
# Some hotkeys for tmux
if [[ -x "$(command -v tmux)" ]]; then
alias t="tmux"
alias ta="t a -t"
alias tls="t ls"
alias tn="t new -t"
fi
# Search through history with arrow keys
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
# A simple calculator in terminal
calc() {
if [ $# -eq 0 ]; then
# replace comma in numblcok with point on german keyboard
xmodmap -e "keycode 91 mod2 = KP_Delete period"; bc -ql; xmodmap -e "keycode 91 mod2 = KP_Delete comma"
else
echo "scale=3;$@" | bc -l
fi
}
# Configure xclip
if [[ -x "$(command -v xclip)" ]]; then
# copy to clipboard
alias xc="xclip -se c"
# copy to pirmary buffer
alias xb="xclip"
fi
# Source node-version manager
if [[ -d "/usr/share/nvm" ]]; then
. "/usr/share/nvm/init-nvm.sh"
fi
# Load fzf stuff
if [[ -d "/usr/share/fzf" ]]; then
. "/usr/share/fzf/completion.bash"
. "/usr/share/fzf/key-bindings.bash"
# We use an improved version of this function, because we use breadth-first
# search by using bfs instead of find
unset -f __fzf_cd__
function __fzf_cd__() {
local cmd dir
cmd="${FZF_ALT_C_COMMAND:-"command bfs -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
-o -type d -print 2> /dev/null | cut -b3-"}"
dir=$(eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS" $(__fzfcmd) +m) && printf 'cd -- %q' "$dir"
}
# Bind "Change Dir" to C-SPACE
bind "$(bind -s | grep '^"\\ec"' | sed 's/\\ec/\\C- /')"
FZF_COMPLETION_TRIGGER='--'
fi
# Source git-prompt and set PS1
if [[ -e "$HOME/.git-prompt.sh" ]]; then
. "$HOME/.git-prompt.sh"
# Set prompt_command to display git informationSource
PROMPT_COMMAND='__git_ps1 "\[\033[38;5;33m\]\u@\h\[\033[0m\]\w" "\\\$ "'
#Some git prompt modification
GIT_PS1_SHOWCOLORHINTS=true
GIT_PS1_SHOWUPSTREAM="auto"
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWSTASHSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
else
PS1='\[\033[38;5;33m\]\u@\h\[\033[0m\]\w$ '
fi
# Set bash tab complete behavior
bind "TAB:menu-complete"
bind "set show-all-if-ambiguous on"
bind "set menu-complete-display-prefix on"