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 set -A complete_got_1 -- \
57 init \
58 import im \
59 clone cl \
60 fetch fe \
61 checkout co \
62 update up \
63 status st \
64 log \
65 diff di \
66 blame bl \
67 tree tr \
68 ref \
69 branch br \
70 tag \
71 add \
72 remove rm \
73 patch pa \
74 revert rv \
75 commit ci \
76 send se \
77 cherrypick cy \
78 backout bo \
79 rebase rb \
80 histedit he \
81 integrate ig \
82 merge mg \
83 stage sg \
84 unstage ug \
85 cat \
86 info
88 Tweak the output of ls
90 alias ls='ls -F'
92 reset(1) doesn't work as expected inside tmux: the old output can still
93 be consulted when scrolling. If I, lazy as I am, bother to type "reset"
94 I want to be sure that the history was cleared!
96 if [ -n "$TMUX" ]; then
97 alias reset='reset && tmux clear-history'
98 fi
100 CDPATH is super useful! I even wrote a post about it:
101 https://www.omarpolo.com/post/enjoying-cdpath.html
103 export CDPATH=".:$HOME/w:/usr/ports:/usr/ports/mystuff:$HOME/quicklisp/local-projects"
105 I love to hate gpg! It needs some special treatments to work and this
106 should also (finger crossed!) fix pinentry over ssh. I'm not sure it
107 works though, it's been a while since I've connected remotely to my
108 desktop.
110 export GPG_TTY=$(tty)
111 if [ -n "$SSH_CONNECTION" ]; then
112 export PINENTRY_USER_DATA="USE_CURSES=1"
113 fi
115 The BSDs have this incredibly useful signal available, it's a shame not
116 to use it!
118 stty status ^T
120 I really like my prompt to be as minimal as possible. For some time
121 I've used a single colon `;' as prompt, it's really nice! At the moment
122 thought I'm usign a more plan9-esque percent sign:
124 PS1='% '
126 I got tired of trying to remember the set of flags for nc to walk to
127 Gemini servers, so here we are:
129 # "post" stdin to the gemini server
130 # usage: gem host [port]
131 gem()
133 host="${1:?missing host}"
134 port="${2:-1965}"
135 nc -c -Tnoverify "${host}" "${port}"
138 I think I've stolen these from someone. It makes a copy of the file and
139 launch an editor on the original file, incledibly useful when working
140 with ports (that's why doas!)
142 mgdiff()
144 if [ -z "$1" ]; then
145 printf "%s\n" "USAGE: mgdiff file" >&2
146 return
147 fi
148 doas cp -p "$1" "$1.orig"
149 doas mg "$1"
152 hist is a quick wrapper around history and grep to quickly search for a
153 previous command:
155 hist()
157 if [ -z "$1" ]; then
158 printf "%s\n" "USAGE: hist pattern" >&2
159 return 1
160 fi
161 history 0 | grep "$1"
164 clbin (the site) is a web pastebin that's easy to use from the command
165 line with curl. clbin (the function) is an easy way to share something,
166 just pipe it to clbin and it returns an url.
168 clbin()
170 curl -F 'clbin=<-' https://clbin.com
173 Some aliases I use when working with the OpenBSD port tree:
175 alias m="make"
176 alias mup="make update-patches"
177 alias mupl="make update-plist"
178 alias mpldc="make port-lib-depends-check"
179 alias pbuild="env MAKE_JOBS=5 time make"
180 alias build="pbuild 2>&1 | tee build"
181 alias pclean='make clean="package plist"'
183 And even more aliases:
185 alias mopnew="mdirs ~/Maildir/op | grep -v emacs | mlist -st | mthread -r | mseq -S"
187 for c in com rep fwd bnc; do
188 alias m$c="VISUAL='mg -f auto-fill-mode' m$c"
189 alias o$c="m$c -from 'Omar Polo <op@openbsd.org>'"
190 done
192 And finally some aliases for mq
194 alias pnq="NQDIR=/tmp/ports/ nq "
195 alias pfq="NQDIR=/tmp/ports/ fq "
197 Stuff to use my own purritobin instance
199 : ${P_SERVER=paste.omarpolo.com}
200 : ${P_PORT=42069}
201 : ${P_TIME=week}
202 : ${P_MAXTIME=30}
204 shell client to upload a plaintext message
206 purr() {
207 curl --silent --max-time "${P_MAXTIME}" \
208 --data-binary "@${1:-/dev/stdin}" \
209 "${P_SERVER}:${P_PORT}/${P_TIME}"
212 shell client to upload an encrypted message
214 meow() {
215 key="$(openssl rand -hex 32)"
216 iv="$(openssl rand -hex 16)"
217 url="$(openssl enc -aes-256-cbc -K ${key} -iv ${iv} -e -base64 -A < ${1:-/dev/stdin} | purr)"
218 printf "%s\n" "${url%\/*}/paste.html#${url##*\/}_${key}_${iv}"
219 unset key iv url
222 ...and to decrypt it
224 meowd() {
225 url="$1"
226 baseurl="${url%\/*}"
227 vals="${url##*\#}"
228 paste=$(printf '%s\n' "${vals}" | cut -d_ -f1)
229 key=$(printf '%s\n' "${vals}" | cut -d _ -f2)
230 iv=$(printf '%s\n' "${vals}" | cut -d _ -f3)
231 curl --max-time "${P_MAXTIME}" --write-out "\n" --silent \
232 "${baseurl}/${paste}" | openssl enc -aes-256-cbc \
233 -base64 -d -K ${key} -iv ${iv}
234 unset url baseurl vals paste key iv
237 find(1) is an invaluable tool and I use it all the time. walk is an
238 attempt to build a wrapper around some common usages of find that is a
239 little bit less verbose to use. The name is stolen from 9front, but the
240 implementation is completely different.
242 # usage: walk [dir] [type] [name regexp] [! command to execute]
243 walk()
245 if [ $# -eq 0 ]; then
246 find .
247 return
248 fi
250 local dir=.
251 local type=
252 local name=\*
254 if [ -n "$1" -a -d "$1" ]; then
255 dir="$1"
256 shift
257 fi
259 case "$1" in
260 b|c|d|f|l|p|s)
261 type="-type $1"
262 shift
263 esac
265 if [ -n "$1" -a "x$1" != "x!" ]; then
266 name="$1"
267 shift
268 fi
270 if [ "x$1" = x! ]; then
271 shift
272 fi
274 if [ $# -eq 0 ]; then
275 find "$dir" $type -iname "$name"
276 else
277 find "$dir" $type -iname "$name" -exec "$@" {} +
278 fi