-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot_zshrc
181 lines (163 loc) · 5.21 KB
/
dot_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
# History
HISTFILE=~/.histfile
HISTSIZE=10000
SAVEHIST=10000
# Options
setopt HIST_IGNORE_DUPS # Ignore duplicates only if directly after one another
setopt PROMPT_SUBST # Turn on command substitution in prompt
# Prompt
PROMPT='%B%F{179}%n@%M%f in %F{9}%2~%f $(git_branch 2 -v)at %F{67}%T%f'$'\n''→%b '
# Vim keybind stuff
bindkey -v
bindkey "^[[3~" delete-char
bindkey "^?" backward-delete-char
bindkey "^[[H" beginning-of-line
bindkey "^[[F" end-of-line
# Apparently, tmux gets 1~ and 4~ instead of H and F so add these too
bindkey "^[[1~" beginning-of-line
bindkey "^[[4~" end-of-line
# Something?
zstyle :compinstall filename '/home/daniel/.zshrc'
# Tab completion (incl. exercism)
export fpath=(~/.config/exercism ~/.zsh/completion $fpath)
# Case insensitive tab completion from both "ends" of the word (so "bar" can autocomplete to "foobar").
# Tries to complete the word as typed first, then if it doesn't find a match, tries to ignore case.
# Thanks to https://stackoverflow.com/questions/24226685/have-zsh-return-case-insensitive-auto-complete-matches-but-prefer-exact-matches
zstyle ':completion:*' matcher-list '' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
autoload -Uz compinit && compinit
_comp_options+=(globdots) # Include hidden files
# --- Aliases ---
# General
alias p="sudo pacman"
alias cds='cd $(select_directory)'
alias here='srcfile=$(select_file) && [ $srcfile ] && mv $srcfile .'
alias up='cd ..'
# Git
alias gs="git status"
alias gl="git log --oneline"
# Prettifying
alias ls="ls -ha --color=auto --group-directories-first"
alias grep="grep --color=auto"
alias bat="bat -p --paging=never"
# Other
alias passgen="< /dev/urandom tr -dc '!?#*-_=()/&%'A-Za-z0-9 | head -c 16; echo"
alias testmic="arecord -vvv -f dat /dev/null"
# Environment
export BROWSER="firefox"
export EDITOR="nvim"
export VISUAL="nvim"
# ... and then PATH
export PATH="$PATH:$HOME/scripts:$HOME/bin:$WEBOS_CLI_TV:$HOME/.local/bin"
twmn_path="/home/daniel/Documents/kth/amanuens/en2720/twmn/src/lib/python/"
canvas_path="/home/daniel/Documents/kth/amanuens/en2720/twmn-ctf/lib/python/"
export PYTHONPATH="$PYTHONPATH:$twmn_path:$canvas_path"
# Utility functions
select_any() {
# See the comment in `select_directory`. The only changes here are on the last line
find ~ -type d \( \
\! -name ".config" \
-a -name ".*" \
-o -name "google-cloud-sdk" \
-o -name "node_modules" \
\) -prune \
-o -print | fzf
}
select_file() {
# See the comment in `select_directory`. The only changes here are on the last line
find ~ -type d \( \
\! -name ".config" \
-a -name ".*" \
-o -name "google-cloud-sdk" \
-o -name "node_modules" \
\) -prune \
-o -type f -print | fzf --preview "bat --style=numbers --color=always --line-range :500 {}"
}
select_directory() {
# See https://www.theunixschool.com/2012/07/find-command-15-examples-to-exclude.html for more info
# find all directories in $HOME
# where the name is not ".config"
# but starts with a "."
# or is called one of the following
# [...]
# and ignore/don't look inside them
# print any directories not matched above
find ~ -type d \( \
\! -name ".config" \
-a -name ".*" \
-o -name "google-cloud-sdk" \
-o -name "node_modules" \
\) -prune \
-o -type d -print | fzf
}
git_branch() {
# Backup: resp=$(git branch 2> /dev/null | grep \* | sed -e 's/* \(.*\)/(\1) /')
resp=$(git branch 2>/dev/null | grep \* | sed -e 's/* \(.*\)/\1 /')
[ "$1" ] && col=$1 || col=2
col_resp="%F{$col}$resp%f"
[ "$resp" ] && echo -n "on $col_resp" || echo -n "$col_resp"
}
# Exported functions
mkdirc() {
mkdir "$1" && cd "$1"
}
export mkdirc
edit() {
[ -z "$1" ] && fname=$(fzf --preview "bat --style=numbers --color=always --line-range :500 {}") || fname=$1
[ "$fname" ] && "$EDITOR" "$fname"
}
export edit
move() {
if [ $# -gt 1 ]; then
mv "$@"
elif [ $# = 1 ]; then
src="$1"
else
# Only select source if not already given
src="$(select_any)"
fi
# Always select target
dest="$(select_directory)"
# Validate source
if [ -d "$src" ] || [ -f "$src" ]; then
# Validate dest and move
[ -d "$dest" ] && mv -v "$src" "$dest"
fi
}
export move
# Python venv helper
venv() {
venv_home="$HOME/.venvs"
case $1 in
create)
venv_dir="$venv_home/$2"
python -m venv "$venv_dir"
echo "Created virtual environment in $venv_dir"
;;
list)
venvs=$(find "$venv_home" -maxdepth 1 -mindepth 1 -type d | awk -F'/' '{print $NF}')
if [ -n "$venvs" ]; then
echo "$venvs"
else
echo "No virtual environments found"
fi
;;
source|set|activate)
venv_dir="$venv_home/$2"
src="$venv_dir/bin/activate"
if [ -f "$src" ]; then
. "$src"
else
echo "No virtualenv found for $2"
fi
;;
*) echo "Unknown command: '$1'" ;;
esac
}
export venv
# Syntax highlighting
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
# Suggestions
source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
# Fuzzy-finding history items (https://github.com/joshskidmore/zsh-fzf-history-search)
source /usr/share/zsh/plugins/zsh-fzf-history-search/zsh-fzf-history-search.zsh
bindkey '^ ' autosuggest-accept