About UNIX Philosophy.
“now I am tied to POSIX shell to run this utility. Not just a single program - a collection of programs. That’s not freedom mate!”
People just get mad if I say anything bad UNIX philosophy because of the tribal nature of developers (not gonna lie, I too am a victim of it), so I’m just gonna say stuff, tell you what I think and let the reader (you) come to a conclusion.
The most famous interpretation (yes there are many interpretations) of UNIX philosophy 1 is - do one thing and do it well, handle text streams, as they are universal interface.
One simple VI hacker example (that I am literally using as I type this post to format each paragraph to not exceed 80 character width 2) would be
after/ftplugin/markdown.vim
nnoremap <buffer> ,f :.!fold -s -w 80<CR>
For non-UNIX nerds reference - VI (in my case Vim) takes the current line and
feed it to fold utility and replaces current line with output of the fold
command.
This approach might sound good at plain sight - I am not re-inventing the wheel and using existing utility - you are correct. But this also introduces a lot of moving parts. The previous example is pathetically trivial. An elite VI hacker example straight from my dotfiles would look something like this - which I am not gonna bother to explain.
scripts/wipe-log4j-ass
#!/bin/sh
awk '
BEGIN {
FS = " - "
}
/ INFO | WARN / {
split($1, a, /[[:space:]]+/)
timestamp = a[1] " " a[2]
level = a[3]
class = a[4]
n = split(class, c, ".")
shortclass = c[n]
message = $2
latency = 0
if (match(message, /latency=[0-9]+ms/)) {
latency = substr(message, RSTART + 8, RLENGTH - 10)
}
printf "%s|%s|%s|%s|%d\n",
timestamp, level, shortclass, message, latency
}
' |
sort -t'|' -k5,5nr |
awk -F'|' '{
printf "%-23s %-5s %-20s %6sms %s\n",
$1, $2, $3, $5, $4
}'
after/ftplugin/log.vim
nnoremap <buffer> ,w :%!wipe-log4j-ass<CR>
tl;dr as you can see it involves 3 languages - AWK, VimL, Shell. The UNIX way of doing things has alot of moving parts and is very hack-ey and now I am tied to POSIX shell to run this utility. Not just a single program - a collection of programs. That’s not freedom mate!
In the early days of computing this approach make sense because they HAD to work with primitive tools to get job done3. But in modern day we have sophisticated tools, we don’t have to suffer this much!
In my case I could’ve written wipe-log4j-ass entirely in VimL. Take a look
at this another example straight from my Vim config which I am also am actively
using right now as I type this post - a utility to squeeze gaps between paragraphs.
autoload/harness.vim
vim9script noclear
export def SqueezeLines(): void
const view = winsaveview()
:%s/\n\{3,}/\r\r/e
winrestview(view)
echo 'Squeezed blank lines'
enddef
plugin/harness.vim
nnoremap ,s :call harness#SqueezeLines()<CR>
I would much rather prefer this where everything is written in VimL and it is also easy for me write compared to piping with coreutils and Perl. Yes it doesn’t work outside Vim I know - but I don’t use these outside Vim anyways. I can freely run this wherever I have Vim9 - even on Windows.
Conclusion
90% of my text manipulation scripts are written in VimL and I’m intentionally trying to avoid filter commands as much as I can. (can’t avoid completely as VI hacker-ism is in my DNA at this point).
Not all utilities need to be “shell” per say. Some of them do make sense to be shell script like this badboy
scripts/node-clean
#!/bin/sh
find ~/coding -type d -name node_modules -exec rm -rf {} +
…or this badboy
scripts/extract
#!/bin/sh
if [ -f "$1" ]; then
case "$1" in
*.tar.bz2) tar xjf "$1" ;;
*.tar.gz) tar xzf "$1" ;;
*.tar.xz) tar xJf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xf "$1" ;;
*.tbz2) tar xjf "$1" ;;
*.tgz) tar xzf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.7z) 7z x "$1" ;;
*) echo "'$1' cannot be extracted via extract" ;;
esac
else
echo "'$1' is not a valid file"
fi
(Ok, I’ll stop tossing random scripts at your face)
tl;dr don’t do drugs and don’t make you’re utilities slave to POSIX shell is my opinion. I value freedom more than anything and sometimes it is ok to break a bedrock philosophy (if you know what you’re doing ofcourse).
After all philosophies are man-made stuff anyways!
- Siddeshwar
-
The one I’ve mentioned is Douglas McIlroy interpretation. You can read more about other interpretations here. ↩
-
Not exceeding 80 column width is a common dogma among UNIX nerds - it has historical reasons though. ↩
-
“CAT is the best text editor” is a old meme among UNIX nerds. Just
cat, type shit and closestdinwith Ctrl+D! ↩