Blame


1 7b4d8efb 2022-02-04 op # ksh
2 7b4d8efb 2022-02-04 op
3 7b4d8efb 2022-02-04 op OpenBSD ksh (sometimes called opdksh or oksh) is the default shell on
4 7b4d8efb 2022-02-04 op OpenBSD, and is generally my go-to choince on other systems too. It has
5 7b4d8efb 2022-02-04 op a good ratio of features and simplicity
6 7b4d8efb 2022-02-04 op
7 7b4d8efb 2022-02-04 op if [ "$TERM" = dumb ]; then
8 7b4d8efb 2022-02-04 op PS1='$ '
9 7b4d8efb 2022-02-04 op return
10 7b4d8efb 2022-02-04 op fi
11 7b4d8efb 2022-02-04 op
12 7b4d8efb 2022-02-04 op Enable emacs-like command editing regardless of $EDITOR and csh-like
13 7b4d8efb 2022-02-04 op history expansion with !
14 7b4d8efb 2022-02-04 op
15 7b4d8efb 2022-02-04 op set -o emacs
16 7b4d8efb 2022-02-04 op set -o csh-history
17 7b4d8efb 2022-02-04 op
18 7b4d8efb 2022-02-04 op Talking about history, by default ksh won't store any, which is
19 7b4d8efb 2022-02-04 op unfortunate. I can't live without my C-r working!
20 7b4d8efb 2022-02-04 op
21 7b4d8efb 2022-02-04 op HISTCONTROL=ignoredups:ignorespace
22 7b4d8efb 2022-02-04 op HISTFILE=$HOME/.history
23 7b4d8efb 2022-02-04 op HISTSIZE=10000
24 7b4d8efb 2022-02-04 op
25 7b4d8efb 2022-02-04 op OpenBSD ksh has a limited support for programmed completions through
26 7b4d8efb 2022-02-04 op static lists. The completions are provided via an array called
27 7b4d8efb 2022-02-04 op complete_$progname; or complete_$progname_$nth for the nth argument.
28 7b4d8efb 2022-02-04 op
29 7b4d8efb 2022-02-04 op Here's the completions for ssh and scp:
30 7b4d8efb 2022-02-04 op
31 7b4d8efb 2022-02-04 op HOST_LIST=$(awk '/Host / {print $2}' ~/.ssh/config | xargs echo)
32 7b4d8efb 2022-02-04 op
33 7b4d8efb 2022-02-04 op set -A complete_ssh -- $HOST_LIST
34 7b4d8efb 2022-02-04 op set -A complete_scp -- $HOST_LIST
35 7b4d8efb 2022-02-04 op
36 7b4d8efb 2022-02-04 op and for kill(1) and pkill(1)
37 7b4d8efb 2022-02-04 op
38 7b4d8efb 2022-02-04 op set -A complete_kill_1 -- -9 -HUP -INFO -KILL -TERM
39 7b4d8efb 2022-02-04 op set -A complete_pkill_2 -- -SIGHUP -SIGUSR1 -SIGUSR2 -SIGTERM -SIGKILL
40 7b4d8efb 2022-02-04 op
41 7b4d8efb 2022-02-04 op and for vmd(8) if available
42 7b4d8efb 2022-02-04 op
43 7b4d8efb 2022-02-04 op if pgrep -fq /usr/sbin/vmd; then
44 7b4d8efb 2022-02-04 op set -A complete_vmctl_1 -- console load reload start stop \
45 7b4d8efb 2022-02-04 op reset status send receive
46 7b4d8efb 2022-02-04 op set -A complete_vmctl -- \
47 7b4d8efb 2022-02-04 op $(vmctl status | awk '!/NAME/{printf "%s ", $NF}')
48 7b4d8efb 2022-02-04 op fi
49 7b4d8efb 2022-02-04 op
50 7b4d8efb 2022-02-04 op and for ifconfig(8)
51 7b4d8efb 2022-02-04 op
52 7b4d8efb 2022-02-04 op set -A complete_ifconfig_1 -- $(ifconfig | grep ^[a-z] | cut -d: -f1)
53 7b4d8efb 2022-02-04 op
54 7b4d8efb 2022-02-04 op and for got(1)
55 7b4d8efb 2022-02-04 op
56 7b4d8efb 2022-02-04 op set -A complete_got_1 -- \
57 7b4d8efb 2022-02-04 op init \
58 7b4d8efb 2022-02-04 op import im \
59 7b4d8efb 2022-02-04 op clone cl \
60 7b4d8efb 2022-02-04 op fetch fe \
61 7b4d8efb 2022-02-04 op checkout co \
62 7b4d8efb 2022-02-04 op update up \
63 7b4d8efb 2022-02-04 op status st \
64 7b4d8efb 2022-02-04 op log \
65 7b4d8efb 2022-02-04 op diff di \
66 7b4d8efb 2022-02-04 op blame bl \
67 7b4d8efb 2022-02-04 op tree tr \
68 7b4d8efb 2022-02-04 op ref \
69 7b4d8efb 2022-02-04 op branch br \
70 7b4d8efb 2022-02-04 op tag \
71 7b4d8efb 2022-02-04 op add \
72 7b4d8efb 2022-02-04 op remove rm \
73 52047f1b 2022-03-30 op patch pa \
74 7b4d8efb 2022-02-04 op revert rv \
75 7b4d8efb 2022-02-04 op commit ci \
76 7b4d8efb 2022-02-04 op send se \
77 7b4d8efb 2022-02-04 op cherrypick cy \
78 7b4d8efb 2022-02-04 op backout bo \
79 7b4d8efb 2022-02-04 op rebase rb \
80 7b4d8efb 2022-02-04 op histedit he \
81 7b4d8efb 2022-02-04 op integrate ig \
82 7b4d8efb 2022-02-04 op merge mg \
83 7b4d8efb 2022-02-04 op stage sg \
84 7b4d8efb 2022-02-04 op unstage ug \
85 7b4d8efb 2022-02-04 op cat \
86 7b4d8efb 2022-02-04 op info
87 7b4d8efb 2022-02-04 op
88 7b4d8efb 2022-02-04 op Tweak the output of ls
89 7b4d8efb 2022-02-04 op
90 7b4d8efb 2022-02-04 op alias ls='ls -F'
91 7b4d8efb 2022-02-04 op
92 7b4d8efb 2022-02-04 op reset(1) doesn't work as expected inside tmux: the old output can still
93 7b4d8efb 2022-02-04 op be consulted when scrolling. If I, lazy as I am, bother to type "reset"
94 7b4d8efb 2022-02-04 op I want to be sure that the history was cleared!
95 7b4d8efb 2022-02-04 op
96 7b4d8efb 2022-02-04 op if [ -n "$TMUX" ]; then
97 7b4d8efb 2022-02-04 op alias reset='reset && tmux clear-history'
98 7b4d8efb 2022-02-04 op fi
99 7b4d8efb 2022-02-04 op
100 7b4d8efb 2022-02-04 op CDPATH is super useful! I even wrote a post about it:
101 7b4d8efb 2022-02-04 op https://www.omarpolo.com/post/enjoying-cdpath.html
102 7b4d8efb 2022-02-04 op
103 7b4d8efb 2022-02-04 op export CDPATH=".:$HOME/w:/usr/ports:/usr/ports/mystuff:$HOME/quicklisp/local-projects"
104 7b4d8efb 2022-02-04 op
105 7b4d8efb 2022-02-04 op I love to hate gpg! It needs some special treatments to work and this
106 7b4d8efb 2022-02-04 op should also (finger crossed!) fix pinentry over ssh. I'm not sure it
107 7b4d8efb 2022-02-04 op works though, it's been a while since I've connected remotely to my
108 7b4d8efb 2022-02-04 op desktop.
109 7b4d8efb 2022-02-04 op
110 7b4d8efb 2022-02-04 op export GPG_TTY=$(tty)
111 7b4d8efb 2022-02-04 op if [ -n "$SSH_CONNECTION" ]; then
112 7b4d8efb 2022-02-04 op export PINENTRY_USER_DATA="USE_CURSES=1"
113 7b4d8efb 2022-02-04 op fi
114 7b4d8efb 2022-02-04 op
115 7b4d8efb 2022-02-04 op The BSDs have this incredibly useful signal available, it's a shame not
116 7b4d8efb 2022-02-04 op to use it!
117 7b4d8efb 2022-02-04 op
118 7b4d8efb 2022-02-04 op stty status ^T
119 7b4d8efb 2022-02-04 op
120 7b4d8efb 2022-02-04 op I really like my prompt to be as minimal as possible. For some time
121 7b4d8efb 2022-02-04 op I've used a single colon `;' as prompt, it's really nice! At the moment
122 7b4d8efb 2022-02-04 op thought I'm usign a more plan9-esque percent sign:
123 7b4d8efb 2022-02-04 op
124 7b4d8efb 2022-02-04 op PS1='% '
125 7b4d8efb 2022-02-04 op
126 7b4d8efb 2022-02-04 op I got tired of trying to remember the set of flags for nc to walk to
127 7b4d8efb 2022-02-04 op Gemini servers, so here we are:
128 7b4d8efb 2022-02-04 op
129 7b4d8efb 2022-02-04 op # "post" stdin to the gemini server
130 7b4d8efb 2022-02-04 op # usage: gem host [port]
131 7b4d8efb 2022-02-04 op gem()
132 7b4d8efb 2022-02-04 op {
133 7b4d8efb 2022-02-04 op host="${1:?missing host}"
134 7b4d8efb 2022-02-04 op port="${2:-1965}"
135 7b4d8efb 2022-02-04 op nc -c -Tnoverify "${host}" "${port}"
136 7b4d8efb 2022-02-04 op }
137 7b4d8efb 2022-02-04 op
138 7b4d8efb 2022-02-04 op I think I've stolen these from someone. It makes a copy of the file and
139 7b4d8efb 2022-02-04 op launch an editor on the original file, incledibly useful when working
140 7b4d8efb 2022-02-04 op with ports (that's why doas!)
141 7b4d8efb 2022-02-04 op
142 7b4d8efb 2022-02-04 op mgdiff()
143 7b4d8efb 2022-02-04 op {
144 7b4d8efb 2022-02-04 op if [ -z "$1" ]; then
145 7b4d8efb 2022-02-04 op printf "%s\n" "USAGE: mgdiff file" >&2
146 7b4d8efb 2022-02-04 op return
147 7b4d8efb 2022-02-04 op fi
148 7b4d8efb 2022-02-04 op doas cp -p "$1" "$1.orig"
149 7b4d8efb 2022-02-04 op doas mg "$1"
150 7b4d8efb 2022-02-04 op }
151 7b4d8efb 2022-02-04 op
152 7b4d8efb 2022-02-04 op hist is a quick wrapper around history and grep to quickly search for a
153 7b4d8efb 2022-02-04 op previous command:
154 7b4d8efb 2022-02-04 op
155 7b4d8efb 2022-02-04 op hist()
156 7b4d8efb 2022-02-04 op {
157 7b4d8efb 2022-02-04 op if [ -z "$1" ]; then
158 7b4d8efb 2022-02-04 op printf "%s\n" "USAGE: hist pattern" >&2
159 7b4d8efb 2022-02-04 op return 1
160 7b4d8efb 2022-02-04 op fi
161 7b4d8efb 2022-02-04 op history 0 | grep "$1"
162 7b4d8efb 2022-02-04 op }
163 7b4d8efb 2022-02-04 op
164 04b39415 2022-02-04 op clbin (the site) is a web pastebin that's easy to use from the command
165 04b39415 2022-02-04 op line with curl. clbin (the function) is an easy way to share something,
166 04b39415 2022-02-04 op just pipe it to clbin and it returns an url.
167 04b39415 2022-02-04 op
168 04b39415 2022-02-04 op clbin()
169 04b39415 2022-02-04 op {
170 04b39415 2022-02-04 op curl -F 'clbin=<-' https://clbin.com
171 04b39415 2022-02-04 op }
172 04b39415 2022-02-04 op
173 7b4d8efb 2022-02-04 op Some aliases I use when working with the OpenBSD port tree:
174 7b4d8efb 2022-02-04 op
175 7b4d8efb 2022-02-04 op alias m="make"
176 7b4d8efb 2022-02-04 op alias mup="make update-patches"
177 7b4d8efb 2022-02-04 op alias mupl="make update-plist"
178 0d484cb2 2022-02-17 op alias mpldc="make port-lib-depends-check"
179 76902419 2022-03-30 op alias pbuild="env MAKE_JOBS=5 time make"
180 76902419 2022-03-30 op alias build="pbuild 2>&1 | tee build"
181 8a8ff32c 2022-02-05 op alias pclean='make clean="package plist"'
182 7b4d8efb 2022-02-04 op
183 afa6a1ba 2022-05-23 op This one is pretty sophisticated, I've stolen it from jca@
184 afa6a1ba 2022-05-23 op
185 afa6a1ba 2022-05-23 op # check shared libs version
186 afa6a1ba 2022-05-23 op cshlib() {
187 afa6a1ba 2022-05-23 op local cnt=0
188 afa6a1ba 2022-05-23 op local f
189 afa6a1ba 2022-05-23 op
190 afa6a1ba 2022-05-23 op for f in $(make show=SHARED_LIBS); do
191 afa6a1ba 2022-05-23 op [ "$((cnt++ % 2))" -eq 1 ] && continue
192 afa6a1ba 2022-05-23 op echo '===>' $f
193 afa6a1ba 2022-05-23 op /usr/src/lib/check_sym /usr/local/lib/lib$f.so* \
194 afa6a1ba 2022-05-23 op $(make show=WRKINST)/usr/local/lib/lib$f.so*
195 afa6a1ba 2022-05-23 op done
196 afa6a1ba 2022-05-23 op }
197 afa6a1ba 2022-05-23 op
198 b73f39e1 2022-03-02 op And even more aliases:
199 b73f39e1 2022-03-02 op
200 b73f39e1 2022-03-02 op alias mopnew="mdirs ~/Maildir/op | grep -v emacs | mlist -st | mthread -r | mseq -S"
201 b73f39e1 2022-03-02 op
202 b73f39e1 2022-03-02 op for c in com rep fwd bnc; do
203 8596ea4a 2022-03-30 op alias m$c="VISUAL='mg -f auto-fill-mode' m$c"
204 b73f39e1 2022-03-02 op alias o$c="m$c -from 'Omar Polo <op@openbsd.org>'"
205 b73f39e1 2022-03-02 op done
206 76902419 2022-03-30 op
207 76902419 2022-03-30 op And finally some aliases for mq
208 76902419 2022-03-30 op
209 76902419 2022-03-30 op alias pnq="NQDIR=/tmp/ports/ nq "
210 76902419 2022-03-30 op alias pfq="NQDIR=/tmp/ports/ fq "
211 76902419 2022-03-30 op
212 76902419 2022-03-30 op Stuff to use my own purritobin instance
213 76902419 2022-03-30 op
214 76902419 2022-03-30 op : ${P_SERVER=paste.omarpolo.com}
215 76902419 2022-03-30 op : ${P_PORT=42069}
216 76902419 2022-03-30 op : ${P_TIME=week}
217 76902419 2022-03-30 op : ${P_MAXTIME=30}
218 76902419 2022-03-30 op
219 76902419 2022-03-30 op shell client to upload a plaintext message
220 76902419 2022-03-30 op
221 76902419 2022-03-30 op purr() {
222 76902419 2022-03-30 op curl --silent --max-time "${P_MAXTIME}" \
223 76902419 2022-03-30 op --data-binary "@${1:-/dev/stdin}" \
224 76902419 2022-03-30 op "${P_SERVER}:${P_PORT}/${P_TIME}"
225 76902419 2022-03-30 op }
226 76902419 2022-03-30 op
227 76902419 2022-03-30 op shell client to upload an encrypted message
228 b73f39e1 2022-03-02 op
229 76902419 2022-03-30 op meow() {
230 76902419 2022-03-30 op key="$(openssl rand -hex 32)"
231 76902419 2022-03-30 op iv="$(openssl rand -hex 16)"
232 76902419 2022-03-30 op url="$(openssl enc -aes-256-cbc -K ${key} -iv ${iv} -e -base64 -A < ${1:-/dev/stdin} | purr)"
233 76902419 2022-03-30 op printf "%s\n" "${url%\/*}/paste.html#${url##*\/}_${key}_${iv}"
234 76902419 2022-03-30 op unset key iv url
235 76902419 2022-03-30 op }
236 76902419 2022-03-30 op
237 76902419 2022-03-30 op ...and to decrypt it
238 76902419 2022-03-30 op
239 76902419 2022-03-30 op meowd() {
240 76902419 2022-03-30 op url="$1"
241 76902419 2022-03-30 op baseurl="${url%\/*}"
242 76902419 2022-03-30 op vals="${url##*\#}"
243 76902419 2022-03-30 op paste=$(printf '%s\n' "${vals}" | cut -d_ -f1)
244 76902419 2022-03-30 op key=$(printf '%s\n' "${vals}" | cut -d _ -f2)
245 76902419 2022-03-30 op iv=$(printf '%s\n' "${vals}" | cut -d _ -f3)
246 76902419 2022-03-30 op curl --max-time "${P_MAXTIME}" --write-out "\n" --silent \
247 76902419 2022-03-30 op "${baseurl}/${paste}" | openssl enc -aes-256-cbc \
248 76902419 2022-03-30 op -base64 -d -K ${key} -iv ${iv}
249 76902419 2022-03-30 op unset url baseurl vals paste key iv
250 8695a5d9 2022-06-09 op }
251 8695a5d9 2022-06-09 op
252 8695a5d9 2022-06-09 op a little awk oneliner to show the stats of a unified diff
253 8695a5d9 2022-06-09 op
254 8695a5d9 2022-06-09 op diffstat() {
255 8695a5d9 2022-06-09 op awk '
256 8695a5d9 2022-06-09 op /^\+/ { a++; next }
257 8695a5d9 2022-06-09 op /^\-/ { m++; next }
258 8695a5d9 2022-06-09 op END { printf("additions:\t%d\nremoval:\t%d\n", a, m) }
259 8695a5d9 2022-06-09 op '
260 76902419 2022-03-30 op }
261 76902419 2022-03-30 op
262 7b4d8efb 2022-02-04 op find(1) is an invaluable tool and I use it all the time. walk is an
263 7b4d8efb 2022-02-04 op attempt to build a wrapper around some common usages of find that is a
264 7b4d8efb 2022-02-04 op little bit less verbose to use. The name is stolen from 9front, but the
265 7b4d8efb 2022-02-04 op implementation is completely different.
266 7b4d8efb 2022-02-04 op
267 7b4d8efb 2022-02-04 op # usage: walk [dir] [type] [name regexp] [! command to execute]
268 7b4d8efb 2022-02-04 op walk()
269 7b4d8efb 2022-02-04 op {
270 7b4d8efb 2022-02-04 op if [ $# -eq 0 ]; then
271 7b4d8efb 2022-02-04 op find .
272 7b4d8efb 2022-02-04 op return
273 7b4d8efb 2022-02-04 op fi
274 7b4d8efb 2022-02-04 op
275 7b4d8efb 2022-02-04 op local dir=.
276 7b4d8efb 2022-02-04 op local type=
277 7b4d8efb 2022-02-04 op local name=\*
278 7b4d8efb 2022-02-04 op
279 7b4d8efb 2022-02-04 op if [ -n "$1" -a -d "$1" ]; then
280 7b4d8efb 2022-02-04 op dir="$1"
281 7b4d8efb 2022-02-04 op shift
282 7b4d8efb 2022-02-04 op fi
283 7b4d8efb 2022-02-04 op
284 7b4d8efb 2022-02-04 op case "$1" in
285 7b4d8efb 2022-02-04 op b|c|d|f|l|p|s)
286 7b4d8efb 2022-02-04 op type="-type $1"
287 7b4d8efb 2022-02-04 op shift
288 7b4d8efb 2022-02-04 op esac
289 7b4d8efb 2022-02-04 op
290 7b4d8efb 2022-02-04 op if [ -n "$1" -a "x$1" != "x!" ]; then
291 7b4d8efb 2022-02-04 op name="$1"
292 7b4d8efb 2022-02-04 op shift
293 7b4d8efb 2022-02-04 op fi
294 7b4d8efb 2022-02-04 op
295 7b4d8efb 2022-02-04 op if [ "x$1" = x! ]; then
296 7b4d8efb 2022-02-04 op shift
297 7b4d8efb 2022-02-04 op fi
298 7b4d8efb 2022-02-04 op
299 7b4d8efb 2022-02-04 op if [ $# -eq 0 ]; then
300 7b4d8efb 2022-02-04 op find "$dir" $type -iname "$name"
301 7b4d8efb 2022-02-04 op else
302 7b4d8efb 2022-02-04 op find "$dir" $type -iname "$name" -exec "$@" {} +
303 7b4d8efb 2022-02-04 op fi
304 7b4d8efb 2022-02-04 op }