-
Notifications
You must be signed in to change notification settings - Fork 3
/
zshrc
367 lines (335 loc) · 10.6 KB
/
zshrc
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
# Zsh doesn't read ~/.profile by default, but any environment variables we want
# to share with desktop programs (like gVim) need to live in that file.
emulate sh
source ~/.profile
emulate zsh
# Configure the prompt, which is implemented in Rust in this repo.
setopt PROMPT_SUBST
# Pass the exit status of the previous command as an argument.
PROMPT='$("$DOTFILES/prompt/target/release/prompt" $?)'
if [[ $XDG_SESSION_TYPE != tty ]] ; then
export SOLARIZED=1
fi
alias ta='tmux attach'
alias grep='grep --color=auto'
alias i="ipython --no-confirm-exit"
alias i2="ipython2 --no-confirm-exit"
alias i3="ipython3 --no-confirm-exit"
if [[ -z "$(command -v open)" ]] ; then
alias open="xdg-open"
fi
alias r='cd $(git rev-parse --show-toplevel || echo .)'
alias rp='cd $(realpath .)'
alias sz='source ~/.zshrc'
alias pypi_upload='python setup.py register sdist upload'
alias tohex="python3 -c 'import sys, binascii; print(binascii.hexlify(sys.stdin.buffer.read()).decode())'"
alias fromhex="python3 -c 'import sys, binascii; sys.stdout.buffer.write(binascii.unhexlify(input().strip()))'"
alias yolo="yay -Syu --noconfirm --removemake"
alias scu="systemctl --user"
alias jcu="journalctl --user"
alias timestamp="date +%Y-%m-%d-%H:%M:%S"
alias dr="docker run --tty --interactive --rm"
alias new="ls -lht --color=always | grep -Ev '^total' | head"
alias fa="fd -i --hidden --no-ignore"
alias ncdu="ncdu --color=dark"
# git aliases
alias git="noglob git" # zsh likes to swallow ^ characters
alias gs='git status'
alias gsi='git status --ignored'
alias gd='git diff'
alias gb='git rev-parse --abbrev-ref HEAD'
alias gcamend="git commit --amend --no-edit"
alias gca="gcamend -a"
alias gcff="git clean -dffx"
alias grh='git reset --hard'
alias gpr='git pull --rebase'
alias gfra='git fetch && git rebase --autostash "$(git_upstream_branch_name)"'
alias gout='git log "$(git_upstream_branch_name)".. --oneline'
alias goutp='git log "$(git_upstream_branch_name)".. -p'
alias ginit='git init && git add -A && git commit -m "first commit"'
alias glog='git log --oneline --decorate --graph'
alias gloga='git log --oneline --decorate --graph --exclude=refs/stash --all'
alias gref='git reflog --all --date=relative'
alias gsub='git submodule update --init --recursive'
alias gfp='git fetch --all --tags --prune'
alias good='git bisect good'
alias bad='git bisect bad'
alias grecent='git for-each-ref --sort=-committerdate refs/heads/ --format="%(refname:short) (%(committerdate:relative))"'
function gdrop() {
local current_branch="$(git symbolic-ref --short HEAD)" &&
git checkout "$(git_main_branch_name)" &&
git branch -D "$current_branch" &&
}
function gpo() {
branch="$(git name-rev --name-only HEAD)"
if [[ "$branch" = "$(git_main_branch_name)" ]] ; then
echo "BLERG! Did you mean to run this on $(git_main_branch_name)?"
return 1
fi
git push origin "$(git name-rev --name-only HEAD)" "$@"
}
function gup() {
git fetch --all --tags --prune && \
if [[ -n "$(git log HEAD.."$(git_upstream_branch_name)" -1)" ]] ; then
git rebase "$(git_upstream_branch_name)" --autostash
else
echo Up to date.
fi
}
function c() {
if [ -t 0 ] && [ -z "$1" ] ; then
echo "BLERG! Tried to copy terminal input."
return 1
fi
xclip -i -selection clipboard "$@"
}
function hl() {
# https://unix.stackexchange.com/a/367/23305
grep --color -E "$1|$" "${@:2}"
}
function git_main_branch_name() {
if git rev-parse origin/main > /dev/null 2>&1 ; then
echo main
return 0
elif git rev-parse origin/master > /dev/null 2>&1 ; then
echo master
return 0
else
echo "Can't figure out main branch name."
return 1
fi
}
function git_upstream_branch_name() {
echo "origin/$(git_main_branch_name)"
}
alias ct='tmux show-buffer | c'
alias v='xclip -o -selection clipboard'
alias ri='rg -i'
alias ra='rg --hidden --no-ignore -L'
alias rai='rg --hidden --no-ignore -L -i'
# State-saving commands for git. The `git clean` at the end of the last two is
# to handle a weird bug where inner git directories aren't removed (though the
# files they contain are removed).
alias save='git add -A && git commit --allow-empty -qnm "SAVE" && git reset --mixed -q HEAD^'
alias wipe='git add -A && git commit --allow-empty -qnm "WIPE" && git reset --hard -q HEAD^ && git clean -dqff'
alias nuke='git add -Af && git commit --allow-empty -qnm "NUKE" && git reset --hard -q HEAD^ && git clean -dqffx'
alias deflate='python3 -c "import zlib,sys;sys.stdout.buffer.write(zlib.compress(sys.stdin.buffer.read()))"'
alias inflate='python3 -c "import zlib,sys;sys.stdout.buffer.write(zlib.decompress(sys.stdin.buffer.read()))"'
venv() {
dir="$(mktemp -d --tmpdir venv.XXX)"
echo "$dir (/tmp/venv)"
ln -snf "$dir" /tmp/venv
target_python="${1:-python}"
real_python="$(basename "$(realpath "$(which "$target_python")")")"
virtualenv "$dir" -p "$real_python" --prompt "[$real_python] "
source "$dir/bin/activate"
}
alias vact="source /tmp/venv/bin/activate"
scratchdir() {
mkdir -p /tmp/scratch
mktemp -d --tmpdir="/tmp/scratch" "$1.XXX"
}
newgo() {
dir="$(scratchdir go)"
ln -sfn "$dir" /tmp/scratch/lastgo
cd "$dir"
cat << EOF > test.go
package main
import (
"fmt"
)
func main() {
fmt.Println("hello")
}
EOF
cat << EOF > go.mod
module test
EOF
"$EDITOR" test.go
}
newrust() {
crate="$(scratchdir rust)"
ln -sfn "$crate" /tmp/scratch/lastrust
cd "$crate"
cargo init --bin --name scratch
git add -A
git commit -m "first"
"$EDITOR" src/main.rs
}
newrustlib() {
crate="$(scratchdir rustlib)"
ln -sfn "$crate" /tmp/scratch/lastrustlib
cd "$crate"
cargo init --lib --name scratch
git add -A
git commit -m "first"
"$EDITOR" src/lib.rs
}
newtokio() {
crate="$(scratchdir tokio)"
ln -sfn "$crate" /tmp/scratch/lasttokio
cd "$crate"
cargo init --bin --name scratch
cargo add futures tokio --features tokio/full
cat << EOF > src/main.rs
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() {
sleep(Duration::from_secs(1)).await;
println!("done");
}
EOF
git add -A
git commit -m "first"
setopt LOCAL_OPTIONS NO_NOTIFY NO_MONITOR
cargo build > /dev/null 2>&1 &
"$EDITOR" src/main.rs
}
newcpp() {
dir="$(scratchdir cpp)"
ln -sfn "$crate" /tmp/scratch/lastcpp
cd "$dir"
ln -sfn "$DOTFILES/clang-format" .clang-format
cat << EOF > scratch.cpp
#include <cstdint>
#include <print>
#include <string>
#include <vector>
int main() {
std::println("{}", 42);
}
EOF
cat << EOF > Makefile
run: scratch
./scratch
scratch: scratch.cpp Makefile
g++ scratch.cpp -o scratch -g -std=c++23 -fsanitize=undefined,address
clean:
rm scratch
EOF
git init
git add -A
git commit -m "first"
"$EDITOR" scratch.cpp
}
cbturbo() {
if [[ "$(cat /sys/devices/system/cpu/intel_pstate/no_turbo)" != 0 ]] ; then
echo "TurboBoost is already off." 1>&2
return 1
fi
BUILD_ARGS=()
for arg in "$@" ; do
if [[ "$arg" = --features* || "$arg" = --all-features || "$arg" = --no-default-features ]] ; then
BUILD_ARGS+=("$arg")
fi
done
cargo +nightly build --benches --release "$BUILD_ARGS[@]" || return $?
restore() {
if [[ "$(cat /sys/devices/system/cpu/intel_pstate/no_turbo)" != 0 ]] ; then
echo 0 | sudo tee /sys/devices/system/cpu/intel_pstate/no_turbo > /dev/null
echo "--- TurboBoost on. ---"
fi
}
trap restore EXIT INT TERM
echo "--- TurboBoost off. ---"
echo 1 | sudo tee /sys/devices/system/cpu/intel_pstate/no_turbo > /dev/null
cargo +nightly bench "$@"
}
# ack is called ack-grep in ubuntu
if (( ! $+commands[ack] )) && (( $+commands[ack-grep]))
then
alias ack=ack-grep
fi
# disable ctrl-s/crtl-q flow control
stty stop undef
# colors for ls
if [[ -n $SOLARIZED && -n "$(command -v dircolors)" ]] ; then
eval $(dircolors "$DOTFILES/dircolors-solarized/dircolors.ansi-dark")
fi
if ls --color=auto > /dev/null 2>&1 ; then
# GNU ls supports --color
alias ls="ls --color=auto"
else
# --color not supported, assume BSD ls
export CLICOLOR="true"
fi
# use emacs keybindings
bindkey -e
# keep vi-mode's Ctrl-W behavior
bindkey '^w' vi-backward-kill-word
# emulate the Ctrl-U behavior from bash
bindkey '^u' backward-kill-line
# add a keybinding to open the $EDITOR
autoload edit-command-line
zle -N edit-command-line
bindkey '^x^e' edit-command-line
# file rename magick
bindkey "^[m" copy-prev-shell-word
# get shared history all working properly
HISTFILE=$HOME/.zsh_history
HISTSIZE=100000
SAVEHIST=$HISTSIZE
setopt extended_history
setopt hist_ignore_dups
setopt hist_ignore_space
setopt inc_append_history
setopt share_history
# use regexes in history search
bindkey '^r' history-incremental-pattern-search-backward
bindkey '^s' history-incremental-pattern-search-forward
autoload -U compinit && compinit
setopt complete_in_word
zstyle ':completion:*' menu select
# LS_COLORS set by dircolors above
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
# case-insensitive (lower only), partial-word, and substring completion
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
# fix Shift-Tab in the completions menu
bindkey '^[[Z' reverse-menu-complete
setopt notify # immediate job notifications
setopt extendedglob # crazy file globbing
setopt autopushd # cd works like pushd
autoload -U zmv
# get insert/delete/home/end working properly
# https://wiki.archlinux.org/index.php/Zsh#Key_Bindings
typeset -A key
key[Home]=${terminfo[khome]}
key[End]=${terminfo[kend]}
key[Insert]=${terminfo[kich1]}
key[Delete]=${terminfo[kdch1]}
[[ -n "${key[Home]}" ]] && bindkey "${key[Home]}" beginning-of-line
[[ -n "${key[End]}" ]] && bindkey "${key[End]}" end-of-line
[[ -n "${key[Insert]}" ]] && bindkey "${key[Insert]}" overwrite-mode
[[ -n "${key[Delete]}" ]] && bindkey "${key[Delete]}" delete-char
# Load settings specific to this machine.
local_zshrc="$HOME/.zshrc.local"
if [ -e "$local_zshrc" ] ; then
source "$local_zshrc"
fi
# FZF solarized colorscheme
# https://github.com/junegunn/fzf/wiki/Color-schemes#alternate-solarized-lightdark-theme
_gen_fzf_default_opts() {
local base03="#002b36"
local base02="#073642"
local base01="#586e75"
local base00="#657b83"
local base0="#839496"
local base1="#93a1a1"
local base2="#eee8d5"
local base3="#fdf6e3"
local yellow="#b58900"
local orange="#cb4b16"
local red="#dc322f"
local magenta="#d33682"
local violet="#6c71c4"
local blue="#268bd2"
local cyan="#2aa198"
local green="#859900"
export FZF_DEFAULT_OPTS="
--color fg:-1,bg:-1,hl:$blue,fg+:$base02,bg+:$base2,hl+:$blue
--color info:$yellow,prompt:$yellow,pointer:$base03,marker:$base03,spinner:$yellow
"
}
_gen_fzf_default_opts
# FZF keybinds (particularly ctrl-r for history search)
source "$DOTFILES/fzf/shell/key-bindings.zsh"