-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path.zshenv
More file actions
194 lines (167 loc) · 4.94 KB
/
Copy path.zshenv
File metadata and controls
194 lines (167 loc) · 4.94 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
#
# ~/.zshenv
#
# This file is sourced by *every* zsh invocation:
# - interactive shells
# - non-interactive shells
# - tmux panes
# - SSH commands
# - scripts using /usr/bin/env zsh
#
# Therefore:
# ✔ keep it FAST
# ✔ avoid heavy external commands
# ✔ define environment + universal utilities only
#
# ZDOTDIR must come before XDG so zsh finds .zshrc in ~/.config/zsh/
export ZDOTDIR="$HOME/.config/zsh"
# ============================================================
# XDG Base Directory Specification
# ------------------------------------------------------------
# Provide defaults when variables are unset.
# Many applications assume these paths implicitly,
# but shells do NOT auto-expand them.
# ============================================================
export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
export XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}"
### zsh completion cache (XDG)
# Store compinit dump files under XDG cache to avoid cluttering $HOME.
export ZSH_CACHE_HOME="${ZSH_CACHE_HOME:-$XDG_CACHE_HOME/zsh}"
export ZSH_COMPDUMP="${ZSH_COMPDUMP:-$ZSH_CACHE_HOME/zcompdump}"
### zsh history location (XDG)
# Store shell history under XDG state directory.
export ZSH_STATE_HOME="${ZSH_STATE_HOME:-$XDG_STATE_HOME/zsh}"
export ZSH_HISTFILE="${ZSH_HISTFILE:-$ZSH_STATE_HOME/history}"
### less history location (XDG)
# Store less history under XDG cache directory (avoid ~/.lesshst in $HOME).
export LESSHISTFILE="${LESSHISTFILE:-$XDG_CACHE_HOME/less/history}"
# ============================================================
# PATH configuration
# ------------------------------------------------------------
# Use zsh's `path` array instead of editing PATH directly.
#
# Advantages:
# - prevents duplicated entries
# - preserves existing system order
# - safe across subshell reloads
# ============================================================
typeset -U path
path=(
"$HOME/.local/bin" # user-local executables
"$HOME/go/bin" # Go install binaries
$path
)
export PATH
### SSH agent consistency
# Ensure SSH_AUTH_SOCK is always exported so tmux panes and
# child shells can access the same SSH agent.
#
# Prefer the persistent GNOME Keyring ssh-agent socket (systemd
# user unit gcr-ssh-agent.socket) over spawning a new `ssh-agent`
# per shell. Keys added via ssh-add are remembered across shells.
_gcr_ssh_sock="$XDG_RUNTIME_DIR/gcr/ssh"
if [[ -S "$_gcr_ssh_sock" ]]; then
export SSH_AUTH_SOCK="$_gcr_ssh_sock"
elif [[ -n "$SSH_AUTH_SOCK" ]]; then
export SSH_AUTH_SOCK
fi
unset _gcr_ssh_sock
# ============================================================
# Cross-platform clipboard utilities
# ------------------------------------------------------------
# Provide macOS-like `pbcopy` / `pbpaste` everywhere.
#
# Supported environments:
# macOS → pbcopy / pbpaste
# Wayland → wl-copy / wl-paste
# X11 → xclip
# Remote → OSC52 escape sequence fallback
#
# Goal:
# Same clipboard workflow across:
# - local terminals
# - tmux
# - SSH sessions
# - remote servers
#
# NOTE:
# Functions are defined here but NOT executed,
# keeping .zshenv lightweight.
# ============================================================
# Detect operating system (POSIX-safe)
_pb_os() {
uname 2>/dev/null
}
# Check command availability
_pb_has() {
command -v "$1" >/dev/null 2>&1
}
# OSC52 clipboard fallback
# Sends clipboard data to terminal emulator.
# Works over SSH/tmux if terminal supports OSC52.
_pbcopy_osc52() {
if _pb_has base64; then
base64 | tr -d '\n' \
| sed 's/^/\033]52;c;/' \
| sed 's/$/\a/'
elif _pb_has openssl; then
openssl base64 -A \
| sed 's/^/\033]52;c;/' \
| sed 's/$/\a/'
else
printf '%s\n' \
"pbcopy: base64 or openssl required for OSC52 fallback" >&2
return 1
fi
}
# ------------------------------------------------------------
# pbcopy — write stdin to system clipboard
# ------------------------------------------------------------
pbcopy() {
case "$(_pb_os)" in
Darwin)
if _pb_has pbcopy; then
command pbcopy
return
fi
;;
esac
# Wayland clipboard
if _pb_has wl-copy; then
wl-copy
return
fi
# X11 clipboard
if _pb_has xclip; then
xclip -selection clipboard
return
fi
# Terminal fallback
_pbcopy_osc52
}
# ------------------------------------------------------------
# pbpaste — read system clipboard to stdout
# ------------------------------------------------------------
pbpaste() {
case "$(_pb_os)" in
Darwin)
if _pb_has pbpaste; then
command pbpaste
return
fi
;;
esac
if _pb_has wl-paste; then
wl-paste
return
fi
if _pb_has xclip; then
xclip -selection clipboard -o
return
fi
printf '%s\n' \
"pbpaste: clipboard read unsupported in this environment" >&2
return 1
}