Recently Written · git

tmpl

Insanely simple templates

git clone https://github.com/equwal/tmpl

Log | Files | Refs


commit 35607bffb05b9a95d29b82ae8ef91d4a99c564c1
Spenser Truex <truex@equwal.com>
2025-09-10 20:05:30 -0700

Added redirection shell hacks

 README.md      |  5 +++++
 sh/redirection | 31 +++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)
diff --git a/README.md b/README.md
index 7e7805a..da95e8e 100644
--- a/README.md
+++ b/README.md
@@ -42,3 +42,8 @@ the `<++>` spots.
     -- Templating shortcut
     map("n", "<leader><Space>", "/<++><CR>dt>a<BS>", { desc = "Jump to next template placeholder" })
 ```
+
+POSIX shell tricks
+Many of the shell tricks taken from here:
+https://www.etalabs.net/sh_tricks.html
+Especially for-find, a nightmare fixed-point solution.
diff --git a/sh/redirection b/sh/redirection
new file mode 100644
index 0000000..c62aff5
--- /dev/null
+++ b/sh/redirection
@@ -0,0 +1,31 @@
+# Cursed shell redirection
+
+<++> 2>&1 # stderr goes wherever stdout is going
+
+# Order matters
+<++> >/dev/null 2>&1 # silence
+nohup <++> >/dev/null 2>&1 # keeps going even if you hang up (close) the terminal. anti-trap.
+nohup <++> >/dev/null 2>&1 & # and async
+<++> >output.log 2>error.log
+<++> >output.log 2>&1 | tee combined.log
+<++> 2>&1 | tee combined.log >/dev/null # only stderr
+
+# File descriptor swapping
+<++> 3>&1 1>&2 2>&3 3>&- # swap stdout and stderr using fd3
+{ <++>; } 2>&1 # group commands
+
+# POSIX-compliant error filtering (replaces process substitution)
+<++> 2>&1 >/dev/null | grep ERROR >&2  # filter stderr through grep, discard stdout
+<++> 2>/tmp/stderr.$$ >/dev/null; grep ERROR < /tmp/stderr.$$ >&2; rm -f /tmp/stderr.$$  # alternative using temp file
+
+# Tests/examples:
+#
+# ls /nonexistent 2>&1
+# ls /nonexistent >/dev/null 2>&1
+# nohup echo "test message" >/dev/null 2>&1
+# nohup sleep 1 >/dev/null 2>&1 & echo "Background job started"
+# { echo "stdout message"; echo "stderr message" >&2; } >output.log 2>error.log; cat output.log; cat error.log; rm output.log error.log
+# { echo "stdout"; echo "stderr" >&2; } >output.log 2>&1 | tee combined.log; cat combined.log; rm output.log combined.log
+# { echo "stdout"; echo "stderr" >&2; } 2>&1 | tee combined.log >/dev/null; cat combined.log; rm combined.log
+# { echo "stdout"; echo "stderr" >&2; } 3>&1 1>&2 2>&3 3>&-
+# { echo "stdout"; echo "stderr" >&2; } 2>&1