-
Notifications
You must be signed in to change notification settings - Fork 1
/
.bashrc
186 lines (153 loc) · 6.28 KB
/
.bashrc
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
# .bashrc
# Source global definitions.
# TODO: Is this necessary? I think /etc/profile is loaded first, which loads /etc/bash.bashrc, then this?
test -f /etc/bash.bashrc && . /etc/bash.bashrc
# User specific aliases and functions.
################################################################################
# Append to history file instead of overwriting it.
shopt -s histappend
# Check the window size after every command.
shopt -s checkwinsize
# Erase duplicates from bash history
HISTCONTROL=ignoredups:erasedups
HISTFILESIZE=10000
HISTSIZE=10000
# Parse out the branch we're on, or return an empty string. Errors are ignored.
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \[\1\]/'
}
# Show exit codes in the command prompt, if the code is not zero.
# Inspired by https://lobste.rs/s/qgqssl/what_are_most_useful_aliases_your_bashrc#c_xa98gj
function show_exit_code {
exit_code=$? # Catch exit code
if [[ $exit_code -ne 0 ]]; then
echo -e "[$exit_code] "
fi
}
# Prompt is '[exit_code] user@host [branch](in red) directory_name$ '.
PS1="\[\033[0;31m\]\$(show_exit_code)\[\033[1;33m\]\u@\H\[\033[0;31m\]\$(parse_git_branch)\[\033[0m\] \W\$ "
# If AWS credentials have been updated in the last 12 hours, TKI is probably ok.
function tki_status {
if [[ -e "$HOME/.aws/credentials" ]]; then
date_format="%Y%m%d%H%M%S" # Reduces time to a comparable number, not epoch dependant.
mod_time=$(date --reference="$HOME/.aws/credentials") # Time AWS creds were modified.
expire_time=$(date --date="$mod_time + 43200 seconds" +"$date_format") # Add 12 hours.
if [[ "$expire_time" -ge "$(date +$date_format)" ]]; then
echo -e "\001\033[0;32m\002TKI:✓ " # Green
else
echo -e "\001\033[0;31m\002TKI:× " # Red
fi
fi
}
# Add the TKI Status to the prompt only if enabled.
CHECK_TKI='false'
if [[ $CHECK_TKI == 'true' ]]; then
PS1="\$(tki_status)$PS1"
fi
# Grep should use perl regexps, be recursive, ignore case, and print line numbers. In that order.
alias g='grep -Prin --color=auto'
# Use ripgrep, if installed. https://github.com/BurntSushi/ripgrep
if which rg 1> /dev/null 2> /dev/null; then
alias grep='rg'
# ripgrep uses Perl(-like) regexps, is recursive, and colorful by default.
alias g='rg -in'
fi
# Use dircolors to setup colors for ls.
if [[ -x /usr/bin/dircolors ]]; then
if [[ -f "${HOME}/.dircolors" ]]; then
eval "$(dircolors -b ~/.dircolors)"
else
eval "$(dircolors -b)"
fi
fi
# ls Should list everything except '.' and '..' in long form, and have color.
alias ls='ls -Alh --color=auto'
# tree should act like ls.
alias tree='tree -Ca'
# getmod gets the mode of a file, without having to look up stat.
alias getmod='stat --format="%a(%A) %N"'
# getown get the owner and group of a file, without having to look up stat
alias getown='stat --format="%u(%U) %g(%G) %N"'
# tmux should assume utf8 and 256 colors.
alias tmux='tmux -u2'
# Try to coerce vim usage when an editor is needed.
export EDITOR=vim
export VISUAL=gvim
export GIT_EDITOR=vim
# Prefer nvim if installed. https://neovim.io/
if which nvim 1> /dev/null 2> /dev/null; then
alias vim='nvim'
export EDITOR=nvim
export GIT_EDITOR=nvim
fi
# Prefer Python3 in virtualenvs, if installed.
if which python3 1> /dev/null 2> /dev/null; then
export VIRTUALENV_PYTHON=python3
fi
# Use git bash completion if it exists.
test -f ~/git-completion.bash && . ~/git-completion.bash
# Use asdf if it is installed
if [[ -d "${HOME}/.asdf" ]]; then
. "${HOME}/.asdf/asdf.sh"
. "${HOME}/.asdf/completions/asdf.bash"
fi
# Use rbenv if it is installed.
# 2017/06/28: rbenv can be installed with `git clone https://github.com/rbenv/rbenv ~/.rbenv`
# ruby-build is necessary to build and install ruby versions. It can be installed with
# `git clone https://github.com/rbenv/ruby-build ~/.rbenv/plugins/ruby-build`
if [[ -d ~/.rbenv/ && ":$PATH:" != *":$HOME/.rbenv/bin:"* ]]; then
export PATH="$PATH:$HOME/.rbenv/bin"
eval "$(rbenv init -)"
fi
# Source Rust's setup, if present.
if [[ -f "${HOME}/.cargo/env" ]]; then
. "${HOME}/.cargo/env"
fi
# Add fits.sh to PATH if it is installed
if [[ -f "$HOME/fits/fits.sh" && ":$PATH:" != *":$HOME/fits:"* ]]; then
export PATH="$PATH:$HOME/fits"
fi
# Ensure the ~/.ssh dir exists, and is only usable by me.
test ! -d "$HOME/.ssh" && mkdir --parents --mode=700 "$HOME/.ssh"
# Tangentially, the correct permissions for SSH keys are 600 for the private key, and 644 for the public (.pub)
# Detect WSL.
if grep -q "[Mm]icrosoft" /proc/version; then
# Make it easy to change to my Windows home.
# -u to convert from Windows to WSL, and -a to force an absolute path.
win_home=$(wslpath -u -a $(wslvar USERPROFILE))
# That path might have a '/', '\r', and/or '\n' at the end. Remove them and add '/workspace/'
wsl_home=$(echo "$win_home" | sed -re 's/\/?\r?\n?$/\/workspace\//g')
# Ensure the workspace dir exists.
test ! -d "$wsl_home" && mkdir --parents "$wsl_home"
alias home="cd \"$wsl_home\""
unset win_home
unset wsl_home
# Use Windows' OpenSSH so you can read keys in 1Password.
# ONLY FOR USE IF ALL KEYS ARE IN 1PASSWORD
#alias ssh='ssh.exe'
#alias ssh-add='ssh-add.exe'
fi
# 2024/06/12 Always set up the agent. With WSL, 1Password cannot import extra keys.
# https://docs.github.com/en/authentication/connecting-to-github-with-ssh/working-with-ssh-key-passphrases#auto-launching-ssh-agent-on-git-for-windows
env="${HOME}/.ssh/agent.env"
agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }
agent_start () {
(umask 077; ssh-agent >| "$env")
. "$env" >| /dev/null ; }
agent_load_env
# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2=agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)
if [[ ! "$SSH_AUTH_SOCK" || $agent_run_state = 2 ]]; then
agent_start
ssh-add
elif [[ "$SSH_AUTH_SOCK" && $agent_run_state = 1 ]]; then
ssh-add
fi
unset env
# End copied script
unset agent_run_state # Script doesn't auto clean up this var.
# AWS environment variables only work when exported. Setting them is not enough.
export AWS_DEFAULT_PROFILE=''
if [[ -d "$HOME/emory-tki/bin" && ":$PATH:" != *":$HOME/emory-tki/bin:"* ]]; then
export PATH=$PATH:"$HOME/emory-tki/bin"
fi