-
Notifications
You must be signed in to change notification settings - Fork 6
/
bashrc
212 lines (171 loc) · 4.93 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
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
##############
# The basics #
##############
[ -z "${PS1:-}" ] && return
umask 0022
# Global settings.
# Prepend cd to directory names automatically
shopt -s autocd 2>/dev/null
# Autocorrect typos in path names when using `cd`
shopt -s cdspell
# explicitly enable term colors.
export TERM="xterm-256color"
ARCH=$(uname -s)
if [ "$ARCH" = "Linux" ]; then
export MAN_POSIXLY_CORRECT=1
COLORS=dircolors
# setup key repeat
xset r rate 180 80
else
# running on a macOS machine.
COLORS=gdircolors
fi
if [ "$(uname -m)" = "arm64" ]
then
brewdir="/opt/homebrew"
else
brewdir="/usr/local"
fi
xiaketDIR=~/.xiaket
etcdir=$xiaketDIR"/etc"
altdir=$xiaketDIR"/alt"
# PATH ordering policy: Alt dir things > My own script > Homebrew > System, bin > sbin
export PATH="$altdir/bin:${HOME}/.xiaket/etc/bin:${HOME}/.xiaket/go/bin:$brewdir/bin:$brewdir/opt/ruby/bin:$brewdir/sbin:/usr/local/bin:/bin:/usr/bin:/usr/sbin:/sbin:${HOME}/.cargo/bin:$brewdir/opt/coreutils/bin:$brewdir/opt/fzf/bin:$HOME/.rye/shims:${HOME}/Library/Python/3.11/bin:${HOME}/.local/bin"
export MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"
export LANG=en_US.UTF-8
# Fix Chinese translation in bash
export LANGUAGE="en_US"
export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/opt/openssl/lib
export XDG_CONFIG_HOME="$etcdir"
############
# sourcing #
#############
# For things that can be used as alias
. "$etcdir"/alias
# For things that can only be done as a bash function.
if [ -f "$etcdir"/bash_functions ]; then
. "$etcdir"/bash_functions
fi
# For Alternative settings
if [ -f "$altdir/etc/bashrc" ]; then
. "$altdir/etc/bashrc"
fi
# for fzf
#set rtp+=/usr/local/opt/fzf
#[ -f $etcdir/fzf/key-binding.sh ] && source $etcdir/fzf/key-binding.sh
# For bash completion.
. "$etcdir"/bash_completion
# I love my prompt
function _xiaket_prompt {
status=$?
PS1="$(ps1 $status)"
history -a
history -n
}
export PROMPT_COMMAND='_xiaket_prompt'
################
# bash history #
################
# don't put duplicate lines in the history. See bash(1) for more options
#export HISTCONTROL=ignoredups
# unlimited playback.
#export HISTFILESIZE=99999
#export HISTSIZE=99999
#export HISTTIMEFORMAT="%h/%d - %H:%M:%S "
# append to the history file, don't overwrite it
#shopt -s histappend
#########################
# environment variables #
#########################
export PYTHONSTARTUP=~/.pythonrc
export PYTHONDONTWRITEBYTECODE="False"
export GOPATH="${xiaketDIR}/go"
# user nvim for everything
export GIT_EDITOR=nvim
export VISUAL=nvim
export EDITOR=nvim
#################
# accessibility #
#################
eval "$("$COLORS" "$HOME/.xiaket/etc/dir_colors")"
# Don’t clear the screen after quitting a manual page.
export MANPAGER='less -X'
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
. "$etcdir"/bash_completion
###########################
# bash history via atuin #
###########################
source "$etcdir/bash-preexec.sh"
ATUIN_SESSION=$(atuin uuid)
export ATUIN_SESSION
_atuin_preexec() {
local id
id=$(atuin history start -- "$1")
export ATUIN_HISTORY_ID="${id}"
}
_atuin_precmd() {
local EXIT="$?"
[[ -z "${ATUIN_HISTORY_ID}" ]] && return
(ATUIN_LOG=error atuin history end --exit "${EXIT}" -- "${ATUIN_HISTORY_ID}" &) >/dev/null 2>&1
export ATUIN_HISTORY_ID=""
}
__atuin_history() {
# shellcheck disable=SC2048,SC2086
HISTORY="$(ATUIN_SHELL_BASH=t ATUIN_LOG=error atuin search $* -i -- "${READLINE_LINE}" 3>&1 1>&2 2>&3)"
if [[ $HISTORY == __atuin_accept__:* ]]
then
HISTORY=${HISTORY#__atuin_accept__:}
echo "$HISTORY"
# Need to run the pre/post exec functions manually
_atuin_preexec "$HISTORY"
eval "$HISTORY"
_atuin_precmd
echo
READLINE_LINE=""
READLINE_POINT=${#READLINE_LINE}
else
READLINE_LINE=${HISTORY}
READLINE_POINT=${#READLINE_LINE}
fi
}
if [[ -n "${BLE_VERSION-}" ]]; then
blehook PRECMD-+=_atuin_precmd
blehook PREEXEC-+=_atuin_preexec
else
precmd_functions+=(_atuin_precmd)
preexec_functions+=(_atuin_preexec)
fi
bind -x '"\C-r": __atuin_history'
bind -x '"\e[A": __atuin_history --shell-up-key-binding'
bind -x '"\eOA": __atuin_history --shell-up-key-binding'
#####################
# ssh agent forward #
#####################
if ls -l ~/.ssh/*.priv >/dev/null 2>&1
then
SSH_ENV="$HOME/.ssh/environment"
function start_agent {
content=$(/usr/bin/ssh-agent | sed "/^echo/d")
[ -f "$SSH_ENV" ] && return 0 || echo "$content" >"$SSH_ENV"
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" >/dev/null
/usr/bin/ssh-add ~/.ssh/*.priv
}
# Source SSH settings, if applicable
[ -d ~/.xiaket/var/tmp ] || mkdir -p ~/.xiaket/var/tmp
lockfile ~/.xiaket/var/tmp/ssh.lock
if [ -f "${SSH_ENV}" ]
then
. "${SSH_ENV}" >/dev/null
if ! pgrep -q "ssh-agent$"
then
rm -f "${SSH_ENV}"
start_agent
fi
else
start_agent
fi
fi
rm -f ~/.xiaket/var/tmp/ssh.lock