-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.zshrc
More file actions
263 lines (228 loc) · 7.52 KB
/
.zshrc
File metadata and controls
263 lines (228 loc) · 7.52 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# ============================================================================
# .zshrc
# ============================================================================
# Copyright (c) 2025 Michael Gardner, A Bit of Help, Inc.
# SPDX-License-Identifier: BSD-3-Clause
# See LICENSE file in the project root.
# ============================================================================
#
# Container-aware Zsh configuration for development environments.
# This configuration helps prevent common mistakes:
# - editing files in the wrong terminal
# - confusing host and container environments
# - forgetting which toolchain path is active
# - debugging mount and user issues more slowly than necessary
#
# It is lightweight, readable, and editor-agnostic.
# ============================================================================
export CHARSET=UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8
export TMPDIR=/tmp
[[ -o interactive ]] || return
[[ "$TERM" = "dumb" ]] && export TERM=xterm-256color
unset PS1 RPS1 RPROMPT PROMPT_COMMAND 2>/dev/null
# Container detection — trust entrypoint-exported markers first, then fall
# back to sentinel files and cgroup inspection for portability.
if [[ -n "$IN_CONTAINER" ]] && (( IN_CONTAINER )); then
# Already set by entrypoint.sh. CONTAINER_RUNTIME is also exported.
:
elif [[ -f /.dockerenv ]]; then
export IN_CONTAINER=1
export CONTAINER_RUNTIME="docker"
elif [[ -f /run/.containerenv ]]; then
export IN_CONTAINER=1
export CONTAINER_RUNTIME="container"
elif grep -qaE '(docker|containerd|kubepods|podman)' /proc/1/cgroup 2>/dev/null; then
export IN_CONTAINER=1
export CONTAINER_RUNTIME="container"
else
export IN_CONTAINER=0
export CONTAINER_RUNTIME=""
fi
# Shell options
setopt AUTO_CD
setopt AUTO_PUSHD
setopt EXTENDED_GLOB
setopt INTERACTIVE_COMMENTS
setopt NO_BEEP
setopt PUSHD_IGNORE_DUPS
# History
HISTFILE="$HOME/.zsh_history"
HISTFILESIZE=999999
HISTSIZE=200000
SAVEHIST=200000
setopt APPEND_HISTORY
setopt EXTENDED_HISTORY
setopt HIST_EXPIRE_DUPS_FIRST
setopt HIST_FIND_NO_DUPS
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_REDUCE_BLANKS
setopt HIST_SAVE_NO_DUPS
setopt INC_APPEND_HISTORY
setopt SHARE_HISTORY
# Completion
export FPATH="$HOME/.docker/completions:$FPATH"
autoload -Uz compinit
compinit -u -d "$HOME/.zcompdump"
zstyle ':completion:*' group-name ''
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*'
zstyle ':completion:*' menu select
zstyle ':completion:*' verbose yes
zstyle ':completion:*:descriptions' format '%B%d%b'
zstyle ':completion:*:messages' format '%d'
zstyle ':completion:*:warnings' format 'No matches for: %d'
# Keybindings + history search
bindkey -e
HSS_LOADED=0
for f in \
/usr/share/zsh-history-substring-search/zsh-history-substring-search.zsh \
/usr/share/zsh/plugins/zsh-history-substring-search/zsh-history-substring-search.zsh
do
if [[ -f "$f" ]]; then
source "$f"
HSS_LOADED=1
break
fi
done
if (( HSS_LOADED )) && zle -l history-substring-search-up >/dev/null 2>&1; then
bindkey '^P' history-substring-search-up
bindkey '^N' history-substring-search-down
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down
bindkey '^[OA' history-substring-search-up
bindkey '^[OB' history-substring-search-down
else
autoload -Uz down-line-or-beginning-search up-line-or-beginning-search
zle -N down-line-or-beginning-search
zle -N up-line-or-beginning-search
bindkey '^P' up-line-or-beginning-search
bindkey '^N' down-line-or-beginning-search
bindkey '^[[A' up-line-or-beginning-search
bindkey '^[[B' down-line-or-beginning-search
bindkey '^[OA' up-line-or-beginning-search
bindkey '^[OB' down-line-or-beginning-search
fi
if command -v fzf >/dev/null 2>&1; then
fzf-history-widget() {
local selected
selected=$(
fc -rl 1 |
sed 's/^[[:space:]]*[0-9]\+[[:space:]]*//' |
fzf --tac --height 40%
) || return
LBUFFER+="$selected"
zle reset-prompt
}
zle -N fzf-history-widget
bindkey '^R' fzf-history-widget
else
bindkey '^R' history-incremental-search-backward
fi
# Prompt
autoload -Uz colors && colors
setopt PROMPT_SUBST
container_tag() {
(( IN_CONTAINER )) || return 0
print -n " %F{blue}[ctr:%B${CONTAINER_RUNTIME}%b]%f"
}
git_branch() {
command git rev-parse --is-inside-work-tree >/dev/null 2>&1 || return 0
local b
b=$(command git symbolic-ref --short HEAD 2>/dev/null) || return 0
print -n " %F{magenta}(${b})%f"
}
# Use DISPLAY_USER (set by entrypoint) to show the intended username in
# rootless mode where whoami returns "root". Falls back to %n (effective user).
PROMPT=$'%(?..%F{red}✗%? %f)%F{green}${DISPLAY_USER:-%n}@%m%f %F{cyan}%~%f$(git_branch)$(container_tag)\n%F{yellow}❯%f '
# Tool environment
export EDITOR="${EDITOR:-nano}"
export GIT_TERMINAL_PROMPT=1
export LESS='-R -F -X'
export PAGER="${PAGER:-less}"
# Aliases — navigation
alias ..='cd ..'
alias ...='cd ../..'
# Aliases — C++ build
alias cb='cmake --build build'
alias cbr='cmake --build build --target clean && cmake --build build'
alias ccfg='cmake -B build -G Ninja'
alias cf='clang-format -i'
alias ct='ctest --test-dir build'
alias ctidy='clang-tidy'
alias mn='meson'
alias nn='ninja'
# Aliases — diagnostics
alias cenv='printenv | sort'
alias cid='cat /etc/hostname'
alias clr='clear'
alias cps='ps -ef'
alias cwho='echo "DISPLAY_USER=${DISPLAY_USER:-$(whoami)}"; id'
alias env='env | sort'
# Aliases — git
alias g='git'
alias gd='git diff'
alias gl='git log --oneline --decorate --graph --max-count=20'
alias gs='git status'
# Aliases — files and search
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
# Python venv helpers
vinit() {
local vpath="${1:-.venv}"
local python_cmd="${2:-python3}"
if ! command -v "$python_cmd" &>/dev/null; then
echo "Error: '$python_cmd' not found"
return 1
fi
if [[ -d "$vpath" ]]; then
echo "Error: '$vpath' already exists"
return 1
fi
"$python_cmd" -m venv "$vpath" && echo "Created: $vpath"
}
vact() {
local dir="$PWD"
local venv_names=(".venv" "env" "venv")
while [[ "$dir" != "/" ]]; do
for venv_name in "${venv_names[@]}"; do
if [[ -f "$dir/$venv_name/bin/activate" ]]; then
source "$dir/$venv_name/bin/activate"
echo "Activated: $dir/$venv_name"
return 0
fi
done
dir="$(dirname "$dir")"
done
echo "No venv found in current or parent directories"
return 1
}
vdact() {
[[ -n "$VIRTUAL_ENV" ]] && deactivate || echo "No venv active"
}
container_info() {
echo "IN_CONTAINER=${IN_CONTAINER}"
echo "CONTAINER_RUNTIME=${CONTAINER_RUNTIME}"
echo "DISPLAY_USER=${DISPLAY_USER:-$(whoami)}"
echo "USER=$(whoami)"
echo "UID=$(id -u)"
echo "GID=$(id -g)"
echo "HOME=$HOME"
echo "PWD=$PWD"
echo "HOSTNAME=$(hostname)"
echo "---"
gcc --version 2>/dev/null | head -1
clang --version 2>/dev/null | head -1
cmake --version 2>/dev/null | head -1
arm-none-eabi-gcc --version 2>/dev/null | head -1
}
# Plugins
[[ -r /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh ]] &&
source /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh
# MUST be last
[[ -r /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh ]] &&
source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh