Blob


1 # ksh
3 OpenBSD ksh (sometimes called opdksh or oksh) is the default shell on
4 OpenBSD, and is generally my go-to choince on other systems too. It has
5 a good ratio of features and simplicity
7 if [ "$TERM" = dumb ]; then
8 PS1='$ '
9 return
10 fi
12 Enable emacs-like command editing regardless of $EDITOR and csh-like
13 history expansion with !
15 set -o emacs
16 set -o csh-history
18 Talking about history, by default ksh won't store any, which is
19 unfortunate. I can't live without my C-r working!
21 HISTCONTROL=ignoredups:ignorespace
22 HISTFILE=$HOME/.history
23 HISTSIZE=10000
25 OpenBSD ksh has a limited support for programmed completions through
26 static lists. The completions are provided via an array called
27 complete_$progname; or complete_$progname_$nth for the nth argument.
29 Here's the completions for ssh and scp:
31 HOST_LIST=$(awk '/Host / {print $2}' ~/.ssh/config | xargs echo)
33 set -A complete_ssh -- $HOST_LIST
34 set -A complete_scp -- $HOST_LIST
36 and for kill(1) and pkill(1)
38 set -A complete_kill_1 -- -9 -HUP -INFO -KILL -TERM
39 set -A complete_pkill_2 -- -SIGHUP -SIGUSR1 -SIGUSR2 -SIGTERM -SIGKILL
41 and for vmd(8) if available
43 if pgrep -fq /usr/sbin/vmd; then
44 set -A complete_vmctl_1 -- console load reload start stop \
45 reset status send receive
46 set -A complete_vmctl -- \
47 $(vmctl status | awk '!/NAME/{printf "%s ", $NF}')
48 fi
50 and for ifconfig(8)
52 set -A complete_ifconfig_1 -- $(ifconfig | grep ^[a-z] | cut -d: -f1)
54 and for got(1)
56 command -v got >/dev/null && \
57 set -A complete_got_1 -- $(got -h 2>&1 | sed -n s/commands://p)
59 Tweak the output of ls
61 alias ls='ls -F'
63 Provide an easiest access to amused
65 alias a=amused
67 reset(1) doesn't work as expected inside tmux: the old output can still
68 be consulted when scrolling. If I, lazy as I am, bother to type "reset"
69 I want to be sure that the history was cleared!
71 if [ -n "$TMUX" ]; then
72 alias reset='reset && tmux clear-history'
73 fi
75 CDPATH is super useful! I even wrote a post about it:
76 https://www.omarpolo.com/post/enjoying-cdpath.html
78 export CDPATH=".:$HOME/w:/usr/ports:/usr/ports/mystuff:$HOME/quicklisp/local-projects"
80 I love to hate gpg! It needs some special treatments to work and this
81 should also (finger crossed!) fix pinentry over ssh. I'm not sure it
82 works though, it's been a while since I've connected remotely to my
83 desktop.
85 export GPG_TTY=$(tty)
86 if [ -n "$SSH_CONNECTION" ]; then
87 export PINENTRY_USER_DATA="USE_CURSES=1"
88 fi
90 The BSDs have this incredibly useful signal available, it's a shame not
91 to use it!
93 stty status ^T
95 I really like my prompt to be as minimal as possible. For some time
96 I've used a single colon `;' as prompt, it's really nice! At the moment
97 thought I'm usign a more plan9-esque percent sign:
99 PS1='% '
101 I got tired of trying to remember the set of flags for nc to walk to
102 Gemini servers, so here we are:
104 # "post" stdin to the gemini server
105 # usage: gem host [port]
106 gem()
108 host="${1:?missing host}"
109 port="${2:-1965}"
110 nc -c -Tnoverify "${host}" "${port}"
113 I think I've stolen these from someone. It makes a copy of the file and
114 launch an editor on the original file, incledibly useful when working
115 with ports (that's why doas!)
117 mgdiff()
119 if [ -z "$1" ]; then
120 printf "%s\n" "USAGE: mgdiff file" >&2
121 return
122 fi
123 doas cp -p "$1" "$1.orig"
124 doas mg "$1"
127 hist is a quick wrapper around history and grep to quickly search for a
128 previous command:
130 hist()
132 if [ -z "$1" ]; then
133 printf "%s\n" "USAGE: hist pattern" >&2
134 return 1
135 fi
136 history 0 | grep "$1"
139 clbin (the site) is a web pastebin that's easy to use from the command
140 line with curl. clbin (the function) is an easy way to share something,
141 just pipe it to clbin and it returns an url.
143 clbin()
145 curl -F 'clbin=<-' https://clbin.com
148 Some aliases I use when working with the OpenBSD port tree:
150 alias m="make"
151 alias mup="make update-patches"
152 alias mupl="make update-plist"
153 alias mpldc="make port-lib-depends-check"
154 alias pbuild="env MAKE_JOBS=5 time make"
155 alias build="pbuild 2>&1 | tee build"
156 alias pclean='make clean="package plist"'
158 This one is pretty sophisticated, I've stolen it from jca@
160 # check shared libs version
161 cshlib() {
162 local cnt=0
163 local f
165 for f in $(make show=SHARED_LIBS); do
166 [ "$((cnt++ % 2))" -eq 1 ] && continue
167 echo '===>' $f
168 /usr/src/lib/check_sym /usr/local/lib/lib$f.so* \
169 $(make show=WRKINST)/usr/local/lib/lib$f.so*
170 done
173 And even more aliases:
175 alias mopnew="mdirs ~/Maildir/op | grep -v emacs | mlist -st | mthread -r | mseq -S; mscan"
177 for c in com rep fwd bnc; do
178 local _mvisual='mg -f auto-fill-mode'
179 if [ -n "$DISPLAY" ]; then
180 _mvisual='emacsclient -c'
181 fi
183 alias m$c="VISUAL='$_mvisual' m$c"
184 alias o$c="m$c -from 'Omar Polo <op@openbsd.org>'"
185 done
187 And finally some aliases for mq
189 alias pnq="NQDIR=/tmp/ports/ nq "
190 alias pfq="NQDIR=/tmp/ports/ fq "
192 Stuff to use my own purritobin instance
194 : ${P_SERVER=paste.omarpolo.com}
195 : ${P_PORT=42069}
196 : ${P_TIME=week}
197 : ${P_MAXTIME=30}
199 shell client to upload a plaintext message
201 purr() {
202 curl --silent --max-time "${P_MAXTIME}" \
203 --data-binary "@${1:-/dev/stdin}" \
204 "${P_SERVER}:${P_PORT}/${P_TIME}"
207 shell client to upload an encrypted message
209 meow() {
210 key="$(openssl rand -hex 32)"
211 iv="$(openssl rand -hex 16)"
212 url="$(openssl enc -aes-256-cbc -K ${key} -iv ${iv} -e -base64 -A < ${1:-/dev/stdin} | purr)"
213 printf "%s\n" "${url%\/*}/paste.html#${url##*\/}_${key}_${iv}"
214 unset key iv url
217 ...and to decrypt it
219 meowd() {
220 url="$1"
221 baseurl="${url%\/*}"
222 vals="${url##*\#}"
223 paste=$(printf '%s\n' "${vals}" | cut -d_ -f1)
224 key=$(printf '%s\n' "${vals}" | cut -d _ -f2)
225 iv=$(printf '%s\n' "${vals}" | cut -d _ -f3)
226 curl --max-time "${P_MAXTIME}" --write-out "\n" --silent \
227 "${baseurl}/${paste}" | openssl enc -aes-256-cbc \
228 -base64 -d -K ${key} -iv ${iv}
229 unset url baseurl vals paste key iv
232 a little awk oneliner to show the stats of a unified diff
234 diffstat() {
235 awk '
236 /^\+/ { a++; next }
237 /^\-/ { m++; next }
238 END { printf("additions:\t%d\nremoval:\t%d\n", a, m) }
242 find(1) is an invaluable tool and I use it all the time. walk is an
243 attempt to build a wrapper around some common usages of find that is a
244 little bit less verbose to use. The name is stolen from 9front, but the
245 implementation is completely different.
247 # usage: walk [dir] [type] [name regexp] [! command to execute]
248 walk()
250 if [ $# -eq 0 ]; then
251 find .
252 return
253 fi
255 local dir=.
256 local type=
257 local name=\*
259 if [ -n "$1" -a -d "$1" ]; then
260 dir="$1"
261 shift
262 fi
264 case "$1" in
265 b|c|d|f|l|p|s)
266 type="-type $1"
267 shift
268 esac
270 if [ -n "$1" -a "x$1" != "x!" ]; then
271 name="$1"
272 shift
273 fi
275 if [ "x$1" = x! ]; then
276 shift
277 fi
279 if [ $# -eq 0 ]; then
280 find "$dir" $type -iname "$name"
281 else
282 find "$dir" $type -iname "$name" -exec "$@" {} +
283 fi