-
Notifications
You must be signed in to change notification settings - Fork 10
/
gibrc
239 lines (207 loc) · 11.9 KB
/
gibrc
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
# vim: filetype=sh
# My rc file, I include this from .profile, .bashrc, and .zshrc like so:
# . "$HOME/.config/gibrc"
# The script must be sourced.
### INITIAL SETUP ###
case $- in
*i*) [ "$GIBRC" ] && echo "Reusing gibrc..." || echo "Using gibrc..." ;;
*) return ;; # Exit if not running interactively.
esac
GIBRC="${GIBRC:-0}" # First time through this is 0, next time 1 (etc).
timer=$SECONDS # Show how long startup took from this point at first prompt.
exists() { which "$1" >/dev/null 2>&1; } # Check if command exists (is in path).
# Internal use variables (shell=current shell, ssh=whether we're sshed).
[ "$SSH_CLIENT" -o "$SSH_TTY" ] && ssh="true" || ssh=""
[ "$ZSH_VERSION" ] && shell=zsh
[ "$BASH_VERSION" ] && shell=bash
case $0 in
-ksh) shell=ksh ;;
-sh) shell=sh ;;
esac
shell=${shell:-"$(ls -l /proc/$$/exe | sed 's%.*/%%')"}
uname="$(uname)"
# If we're sshed into a machine, and didn't manually run a different, try a better shell.
if [ "$ssh" -a "$GIBRC" = 0 -a "$shell" != zsh ]; then
if exists zsh; then
echo "Upgrading to zsh"
export GIBRC=1
exec zsh # Replace existing shell with zsh.
elif [ "$shell" != bash ] && exists bash; then
echo "Upgrading to bash"
export GIBRC=1
exec bash # Replace existing shell with bash.
fi
fi
### ENV VAR DEFAULTS ###
export XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-"$HOME/.config"} # Config stuff should go here.
export XDG_CACHE_HOME=${XDG_CACHE_HOME:-"$HOME/.cache"} # Cache stuff should go here.
export XDG_DATA_HOME=${XDG_DATA_HOME:-"$HOME/.local/share"} # Data should go here.
export LESSHISTFILE=${LESSHISTFILE:-"$XDG_CACHE_HOME/less/history"} # Get less to put its history here.
#export INPUTRC=${INPUTRC:-"$XDG_CONFIG_HOME/inputrc"} # inputrc file goes here (unused currently).
export CARGO_HOME=${CARGO_HOME:-"$XDG_DATA_HOME/cargo"} # Cargo goes here.
export RUSTUP_HOME=${RUSTUP_HOME:-"$XDG_DATA_HOME/rustup"} # Rustup goes here.
export ATOM_HOME=${ATOM_HOME:-"$XDG_CONFIG_HOME/atom"} # Atom goes here.
export npm_config_devdir=${npm_config_devdir:-"$XDG_CACHE_HOME/node-gyp"} # Set node-gyp download location.
export npm_cache=${npm_cache:-"$XDG_CACHE_HOME/npm"} # Set npm cache location.
export NODE_REPL_HISTORY=${NODE_REPL_HISTORY="$XDG_CACHE_HOME/node_repl_history"} # Set node-gyp download location.
export BABEL_CACHE_PATH=${BABEL_CACHE_PATH="$XDG_CACHE_HOME/babel/cache.json"} # Set babel cache location.
export CCACHE_DIR="$XDG_DATA_HOME/ccache" # ccache cache dir.
export HELM_HOME="$XDG_DATA_HOME/helm" # Helm home directory.
export BLUEMIX_HOME="$XDG_DATA_HOME/bluemix" # Bluemix home directory (creates .bluemix inside)
export LESS="--tabs=1,3 --ignore-case --RAW-CONTROL-CHARS" # Tab is 2 spaces, search ignores case, enable colours.
export PAGER=less
HISTSIZE=10000 SAVEHIST=10000 HISTFILE="$XDG_CACHE_HOME/$shell/history" # Set history file location and size.
c="$XDG_CONFIG_HOME" # Shortcut to the config directory (`v $c/gibrc` to edit this file).
[ -d "$XDG_CONFIG_HOME/terminfo" ] && export TERMINFO="$XDG_CONFIG_HOME/terminfo" # Put terminal info in here.
### FUNCTIONS ###
for i in please po pu rd d afind _ md run-help +; do alias | sed 's/^alias //' | grep -q "^$i=" && unalias "$i"; done; unset i # Remove unwanted aliases if defined.
md() { command mkdir -p $*; [ $# = 1 ] && cd "$1" || true; } # md foo = mkdir foo && cd foo.
# Add to beginning of path.
pathadd() { [ -d "$1" ] && export PATH="$1"$(echo ":$PATH:" | sed "s|:$1:|:|" | sed 's|:$||'); }
# Add to end of path.
pathapp() { [ -d "$1" ] && export PATH=$(echo ":$PATH:" | sed "s|:$1:|:|" | sed 's|^:||')"$1"; }
exists fd || fd() { find . -iname "*$**"; } # Find by filename (case insensitive).
pth() { # Returns absolute path to file(s) including file name.
for i in "$@"; do # Args = relative filenames.
if [ -d "$i" ]; then (pushd "$i" >/dev/null; pwd) # dir.
elif [ -f "$i" ]; then # file.
case $i in */*) echo "$(pushd ${i%/*} >/dev/null; pwd)/${i##*/}" ;; *) echo "$PWD/$i" ;; esac
fi
done
}
### PATH ADDITIONS ###
pathadd /usr/local/bin # Missed in some shells.
pathadd "$HOME/bin" # Put random binaries you want to run in here.
pathadd /usr/lib/ccache # Add ccache to the path if installed (Linux).
pathadd /usr/local/opt/ccache/libexec # Add ccache to the path if installed (macOS).
pathadd "$HOME/.local/bin"
pathadd "$CARGO_HOME/bin" # Rust packages.
### SHELL SPECIFIC ###
# Add to end of path.
pathapp /usr/local/opt/coreutils/libexec/gnubin # Coreutils from brew on macOS, don't replace mac ls.
exists gls && alias ls=gls
if ls --color=auto >/dev/null 2>&1; then
exists gls && alias ls='gls --color=auto' || alias ls='ls --color=auto'
if [ -e "$XDG_DATA_HOME"/LS_COLORS/LS_COLORS ]; then
eval "$(dircolors -b "$XDG_DATA_HOME/LS_COLORS/LS_COLORS")" # Load custom LS_COLORS from project.
else
source "$XDG_DATA_HOME/LS_COLORS.old" # Use old version if LS_COLORS not installed.
fi
[ "$LS_COLORS" ] && export LS_COLORS # ls isn't a builtin, so colors must be exported.
elif [ "$uname" = Darwin ]; then
alias ls='ls -G' # Colour ls on macOS.
export LSCOLORS="Gxfxcxdxbxegedabagacad"
fi
[ "$uname" = Darwin ] && alias dl=rmtrash
if [ "$shell" = zsh ]; then # zsh specific options.
export ZDOTDIR="${XDG_CONFIG_HOME}/zsh" # Path to zsh config files.
# cd pushes onto pushd stack (Try `cd -<Tab>`)
# If you type a file name/path by itself, cd to it, don't print dir on cd unless in $CDPATH.
setopt autopushd autocd pushd_silent pushdignoredups pushdminus
setopt prompt_subst # Allow variables and functions to be used in the prompt.
setopt no_list_ambiguous # Show menu on first tab if multiple matches (don't wait for second tab).
setopt no_list_beep # Don't beep if tab-complete has multiple choices, only if no such file.
setopt incappendhistory # Add to history file immediately (don't wait till shell exits).
setopt hist_ignore_all_dups hist_ignore_space share_history extended_history hist_expire_dups_first
setopt always_to_end complete_in_word long_list_jobs # Completion related options.
setopt interactive_comments # Allow comments in the terminal.
chpwd() { ls -A; } # Auto ls after changing directory.
CDPATH=~ # Check ~ for directories after checking . (`c/d/` matches `./c*/d*/`, then tries `~/c*/d*/`).
# Put (or symlink) autocomplete scripts in here.
fpath+=("$XDG_DATA_HOME/zfunc")
# Seems to be a bug in earlier zsh versions.
zsh_v=(${(@s/./)ZSH_VERSION}) # Convert version string to an array.
[ "${zsh_v[0]}" -gt 4 -o "${zsh_v[0]}" -eq 4 -a "${zsh_v[1]}" -gt 3 ] && fpath+=/usr/local/share/zsh-completions # zsh-completions.
for file in "$ZDOTDIR"/{completion-keybindings,gib-theme,macos-setdir}.zsh; do
source "$file" || true
done
[ "$(uname -m)" != s390x ] && source "$XDG_DATA_HOME/zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" || true
elif [ "$shell" = bash ]; then # Bash specific options.
set -o vi # Vim mode in bash.
shopt -s autocd 2>/dev/null # type directory name to cd.
cs() { cd "$@" && ls; } # cs => cd and ls.
cd=cs # Used in .. commands below (so .. does `cs ..`).
CDPATH=:~ # Check ~ for directories after checking . (`c/d/` matches `./c*/d*/`, then tries `~/c*/d*/`).
bind Space:magic-space 2>/dev/null # !31 autoexpands on space.
bind 'set completion-ignore-case on' 2>/dev/null # case-insensitive tab completion.
PS1=' \[\e[32;1m\]\w $ \[\e[0m\]'
fi
if exists fzf; then
for file in "$XDG_DATA_HOME"/fzf/shell/{completion,key-bindings}.$shell; do
source "$file" # 2>/dev/null || true
done
fi
command mkdir -p "$XDG_CACHE_HOME/$shell" # Create HISTFILE dir if necessary.
### ALIASES ###
alias x=exit # x exits the terminal.
alias c=clear # c clears the terminal screen (can also use Ctrl-L).
alias g=git # Use with commands in $c/git/config for best results.
alias path='echo $PATH' # Single quotes so we echo path at runtime, not alias time.
alias h=history # Shows command history.
alias s='printf "\033[2 q" && ssh' # Reset cursor to block and ssh.
alias dt="date +%Y-%m-%d" # Basic date format (unambiguous, easy to sort etc.)
alias dT="date +%d-%b-%y" # More readable date format (ambiguous).
alias cal='cal | grep --color -EC6 "\b$(date +%e | sed "s/ //g")"' # More readable date format (ambiguous).
scpkey() { ssh $1 'mkdir -p ~/.ssh'; scp ~/.ssh/${2:-authorized_keys_scp} $1:~/.ssh/authorized_keys; } # scp key to host.
alias l='ls -lAh' # Hidden, detail, human readable numbers.
alias la='ls -A' # List hidden files as well.
alias ll='ls -l' # Gives more detail about files.
[ -z "$cd" ] && cd=cd # We only want cd=cs in bash.
alias ..="$cd .." # .. -> go up a directory.
alias ...="$cd ../.." # ↳ go up two directories.
alias ....="$cd ../../.." # ↳ go up two directories.
alias .....="$cd ../../../.." # ↳ go up two directories.
alias ......="$cd ../../../../.." # ↳ go up two directories.
alias -- -="$cd -" # - cd's to the last directory.
# Execute previous commands (1 = !-2, 2 = !-3 etc., remember that Enter is !!)
alias 1="fc -e : -2"; alias 2="fc -e : -3"
{ exists nvim && VIM=nvim; } || { exists vim && VIM=vim; } || VIM=vi # Take what you can get.
export VISUAL=${VISUAL:-$VIM} EDITOR=${EDITOR:-$VIM} # Set vim/nvim as the default editor.
alias xv="xargs \$${ZSH_VERSION:+=}VISUAL" v="\$${ZSH_VERSION:+=}VISUAL" # Work around zsh's "helpful" autoquoting.
exists vimr && alias vm=vimr # Use `vm` for GUI vim, need to add a Linux equivalent.
ulimit -c unlimited # Allow saving of coredumps.
alias busy='cat /dev/urandom | hexdump -C | grep "ca fe"' # Makes it look like you're busy with important hacker stuff.
[ "$uname" = "Darwin" ] && alias ldd="otool -L" # macOS equivalent to ldd.
[ "$uname" = "Darwin" ] && { alias cpy="pbcopy"; alias pst="pbpaste"; alias o=open; } # Cross-platform copy/paste/open terminal commands.
[ "$uname" = "Linux" ] && { alias cpy="xclip -selection clipboard"; alias pst="xclip -selection clipboard -o"; alias o=xdg-open; }
# I don't have nvm autorun on every shell, I us `nv` when I want to start it.
# This also allows me to frequently `make -j4 install` node master, and have
# that as the default (in /usr/local/bin/node).
export npm_config_userconfig="$XDG_CONFIG_HOME/npm/config"
export NVM_DIR="$XDG_DATA_HOME/nvm" # Use nvm.
alias nv='. $NVM_DIR/nvm.sh' # Start nvm with alias, o/w too much startup cost.
## Rust commands.
alias cn="cargo new --bin"; alias cl="cargo clippy"
alias cb="cargo build"; alias cr="cargo run"; alias ct="cargo test"
alias ctag='ctags -R . $HOME/bin/src/rust' # Populate ctags from rust src.
alias ru=rustup e=exercism k=kubectl
rs() { for i in $@; do rustc ${i%.rs}.rs; ./${i%.rs}; done; } # Compile/run (rs a.rs b).
exists fd && export FZF_DEFAULT_COMMAND='fd --type f --hidden --follow --exclude .git' # Use fd for fuzzy finding if available.
# Build node with ninja.
alias bn='export CPPFLAGS=-fdiagnostics-color && ./configure && tools/gyp_node.py -f ninja && ninja -C out/Release && ln -fs out/Release/node node'
## Set prompt for SSH windows.
if [ "$ssh" ]; then
case $shell in
zsh|fish) ;; # These should be set elsewhere.
bash) PS1='\[\e[35;1m\]\h \n \w $ \[\e[0m\]' ;;
ksh93) PS1='\E[35;1m '"$(hostname)"' \n $PWD \E[0m $ ' ;;
*) PS1="$(uname -n)"' $PWD $ ' ;;
esac
else # Things to only do if we're not in SSH mode.
# Set key repeat rate if available (Linux only). You probably want something less insane here, like rate 250 30.
if [ "$(uname)" = Linux ]; then
exists xset && xset r rate 120 45
export PANEL_FIFO="/tmp/panel-fifo" # Used by bspwm.
fi
fi
# Basic keys on old shells.
if [ "$shell" = ksh93 -o "$shell" = ksh ]; then
set -o emacs
alias __A=`echo "\020"` # up arrow = ^p = back a command.
alias __B=`echo "\016"` # down arrow = ^n = down a command.
alias __C=`echo "\006"` # right arrow = ^f = forward a character.
alias __D=`echo "\002"` # left arrow = ^b = back a character.
alias __H=`echo "\001"` # home = ^a = start of line.
alias __Y=`echo "\6"` # end = ^e = end of line.
fi
export GIBRC="$(expr $GIBRC + 1)" # Increment GIBRC.