forked from garybernhardt/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.zshrc
161 lines (142 loc) · 4.62 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
# Set custom prompt
setopt PROMPT_SUBST
autoload -U promptinit
promptinit
prompt grb
# Initialize completion
autoload -U compinit
compinit -D
# Add paths
export PATH=/usr/local/sbin:/usr/local/bin:${PATH}
export PATH="$HOME/bin:$PATH"
#export PATH="./node_modules/.bin:$PATH"
# Colorize terminal
alias ls='ls -G'
alias ll='ls -lG'
export LSCOLORS="ExGxBxDxCxEgEdxbxgxcxd"
export GREP_OPTIONS="--color"
# Nicer history
export HISTSIZE=100000
export HISTFILE="$HOME/.history"
export SAVEHIST=$HISTSIZE
# Stop wget from creating ~/.wget-hsts file. I don't care about HSTS (HTTP
# Strict Transport Security) for wget; it's not as if I'm logging into my bank
# with it.
alias wget='wget --no-hsts'
# Use vim as the editor
export EDITOR=vi
# GNU Screen sets -o vi if EDITOR=vi, so we have to force it back.
set -o emacs
# Use C-x C-e to edit the current command line
autoload -U edit-command-line
zle -N edit-command-line
bindkey '\C-x\C-e' edit-command-line
# By default, zsh considers many characters part of a word (e.g., _ and -).
# Narrow that down to allow easier skipping through words via M-f and M-b.
export WORDCHARS='*?[]~&;!$%^<>'
# Highlight search results in ack.
export ACK_COLOR_MATCH='red'
# Aliases
function mkcd() { mkdir -p $1 && cd $1 }
function cdf() { cd *$1*/ } # stolen from @topfunky
# Activate the closest virtualenv by looking in parent directories.
activate_virtualenv() {
if [ -f env/bin/activate ]; then . env/bin/activate;
elif [ -f ../env/bin/activate ]; then . ../env/bin/activate;
elif [ -f ../../env/bin/activate ]; then . ../../env/bin/activate;
elif [ -f ../../../env/bin/activate ]; then . ../../../env/bin/activate;
fi
}
# Find the directory of the named Python module.
python_module_dir () {
echo "$(python -c "import os.path as _, ${1}; \
print _.dirname(_.realpath(${1}.__file__[:-1]))"
)"
}
# By @ieure; copied from https://gist.github.com/1474072
#
# It finds a file, looking up through parent directories until it finds one.
# Use it like this:
#
# $ ls .tmux.conf
# ls: .tmux.conf: No such file or directory
#
# $ ls `up .tmux.conf`
# /Users/grb/.tmux.conf
#
# $ cat `up .tmux.conf`
# set -g default-terminal "screen-256color"
#
function up()
{
local DIR=$PWD
local TARGET=$1
while [ ! -e $DIR/$TARGET -a $DIR != "/" ]; do
DIR=$(dirname $DIR)
done
test $DIR != "/" && echo $DIR/$TARGET
}
# Switch projects
function p() {
local proj
proj=$(ls ~/proj | selecta)
if [[ -n "$proj" ]]; then
cd ~/proj/$proj
if [[ -e "Gemfile" ]]; then
local ruby_version
ruby_version=$(ruby -ne $'print $1 if $_ =~ /ruby [\'"]([0-9.]+)[\'"]/' Gemfile)
chruby "$ruby_version"
gem_home .
fi
if [[ -d ~/secrets/$proj ]]; then
. ~/secrets/$proj/secrets.sh
fi
fi
}
# Edit a note
function note() {
local note=$(find ~/notes/* | selecta)
if [[ -n "$note" ]]; then
(cd ~/notes && vi "$note")
fi
}
# By default, ^S freezes terminal output and ^Q resumes it. Disable that so
# that those keys can be used for other things.
unsetopt flowcontrol
# Run Selecta in the current working directory, appending the selected path, if
# any, to the current command.
function insert-selecta-path-in-command-line() {
local selected_path
# Print a newline or we'll clobber the old prompt.
echo
# Find the path; abort if the user doesn't select anything.
selected_path=$(fd -t f . | selecta) || return
# Escape the selected path, since we're inserting it into a command line.
# E.g., spaces would cause it to be multiple arguments instead of a single
# path argument.
selected_path=$(printf '%q' "$selected_path")
# Append the selection to the current command buffer.
eval 'LBUFFER="$LBUFFER$selected_path "'
# Redraw the prompt since Selecta has drawn several new lines of text.
zle reset-prompt
}
# Create the zle widget
zle -N insert-selecta-path-in-command-line
# Bind the key to the newly created widget
bindkey "^S" "insert-selecta-path-in-command-line"
source /opt/homebrew/share/chruby/chruby.sh
source /usr/local/share/gem_home/gem_home.sh
export PATH="/opt/homebrew/bin:$PATH"
export PATH="/usr/local/opt/node@12/bin:$PATH"
export PATH="/usr/local/opt/postgresql@13/bin:$PATH"
export PATH="/opt/homebrew/opt/node@14/bin:$PATH"
# Autocompletion for custom git aliases that are implemented as shell commands
# (starting with `!` in the git alias). Whatever code handles git-zsh
# autocompletion calls functions named by a convention: to do completion for
# `git foo`, it calls `_git-foo`.
_git-r() {
_git-branch
}
_git-l() {
_git-branch
}