Scripts I wrote that I use all the time
76 points by xavdid
76 points by xavdid
line 10prints line 10 from stdin. For example,cat some_big_file | line 10prints line 10 of a file. This feels like one of those things that should be built in, like head and tail.
sed -n 10p
Couple of things I use all the time:
t which is like /usr/bin/time, but prints human-readable output
λ t ./zig/zig build check
real 1.74s
cpu  1.69s (1.53s user + 159.62ms sys)
rss  207.98mb
n, which runs command n times:
λ n 3 ./zig/zig build fuzz -- vsr_superblock_quorums
Run 1
info(fuzz): Fuzz seed = 1013883851950218510
info(fuzz): done in 6.468ms
Run 2
info(fuzz): Fuzz seed = 17103397363377754742
info(fuzz): done in 6.587ms
Run 3
info(fuzz): Fuzz seed = 11888177293080997501
info(fuzz): done in 6.463ms
mkcd keeps getting reinvented:
https://github.com/fish-shell/fish-shell/issues/9223
It would be fun if a shell would keep a recursive filesystem watch for the current directory and then use the information to update autocompletion suggestions or at least the ordering.
I'm definitely repurposing some of these; especially the clipboard ones.
One of my more esoteric ones is having ripgrep update neovim's last search pattern: https://gist.github.com/coxley/b9618b90196c33fc63d5c9b19aa236ee
Workflow is typically like:
> rg 'some.*pattern'
> vim file-from-list
# Immediately press 'n' to start cycling occurrences
Neat! If you pipe rg -n … | vim - does it also work? Then you could use gF to open the file from list instead of having to type the path out. Not sure if the VimEnter autocmd fires too early, before the search file is created
At this point you're kinda reinventing Vim's builtin grep support.
Bonus tips:
set grepprg=rg\ --vimgrep to use ripgrep, and to avoid having to type out -r or globs. (stolen from):gr <C-r><C-w>.I’m usually using ripgrep for non-vim things, and then want to jump in. I’ll check that out though!
I have LSP keybinds for seeing where functions are used haha.
Around 20 years ago, when I was first learning to program (with O'Reilly's Learning Perl), I wrote a script to pick a haiku at random and print it to the terminal. (I filled it with haikus from Robert Hass's The Essential Haiku: Versions of Basho, Buson, & Issa, which I recommend very highly. I spent far longer typing the poems into the file than writing the script.) It's still my favorite program, and every machine I work on gets a copy in ~/bin.
% haiku
   She's put the child to sleep
and now she washes clothes
   under the summer moon.
    Issa
At a previous job I wrote (and sometimes used) a script to run diff on the output of two commands. That's useful for stuff like diffing the output of some grep ... | sort command for two different input files.
When I need to do this I do
diff <(cmd1) <(cmd2)
Although admittedly all the parens are mildly annoying.
Here's one I use every day to paste  code into a forum that uses markdown -- I call the script reddit but of course it works here and other places too.
#!/bin/sh
expand $1 | perl -pe 's/^/    /'
Once upon a time I had a script to modify in some way (I don't remember) a text to be pasted, but I made it work on the clipboard itself. So something like this (I don't remember if sponge is needed right before xclip -out):
xclip -in | do-the-thing | xclip -out
For some uses a bookmarklet could be useful instead. In case of your script it could work on the selected text, but use whatever is comfortable for you.
Arcan (or rather durden) has a pretty cool feature, where you can globally add a preprocess filter to all clipboard paste operations on the WM level. The "trim spaces or CR/LF" filter from above is one of the builtins.
#!/bin/sh expand $1 | perl -pe 's/^/ /'
Very silly, but when I was trying to learn C, I wrote a version of this in C. I still use it multiple times a week.
sleepybear puts my system to sleep, and works on macOS and Linux. I use this a few times a week.
FreeBSD ships with a script like this out of the box: zzz(8).
I think the idea is to adapt it to all the machines you work on so that you don't need to remember the specific call, just run sleepybear.
But in my experience, if I don't know what kind of machine I'm on, I don't want to put it to sleep -- in fact, pretty much the only time I want to put a machine to sleep is by closing the lid on a laptop.
YMM, obviously, V.
This just makes me realize that I probably severely underutilized scripting for personal tasks. For example, that "getsong" script is something I do manually all the time
I use this about once a month.
IMHO, tools that you use once a month, even simple ones, are not worth it. Yes, it only takes a little time to write, and you save a bit of time each time you use it, but the killer is that you have to use up some of your precious cranial NVRAM to remember that the tool exists. I much prefer to remember how to use general purpose tools, and then (re)assemble them to meet my need on the spot.
For example:
jsonformat takes JSON at stdin and pretty-prints it to stdout. I use this a few times a year.
It's a much better use of my time to learn how to use jq.
Some of us have had these things for longer than the obvious replacement exists.
And yeah, some things in my dotfiles have not been touched for 10y.
jsonformat could be extended to run json_pp as yet another fallback. In my experience json_pp is installed on many systems even when jq and node are not yet installed.