Useful Code Snippets

Inspired by an Evan Hahn article I came across the other day, I thought I should share some snippets I have sitting around on my computer that other people might find useful.

Command Line Utilities§

Aliases§

You can add these to your .bashrc/.zshrc to simplify common tasks.

Quickly Source Your Configuration File§

Terminal window
alias szrc='source ~/.zshrc'

Format Copied JSON Text§

Pastes the contents of your clipboard into the popular json query utility jq, and places it back on the clipboard. This is a quick way to turn unformatted json into formatted json. (The paste commands are Mac OS specific, there will be other utilities besides pbcopy and pbpaste in other environments.)

Terminal window
alias jqcb='pbpaste | jq | pbcopy'

Delete With Extreme Prejudice§

Sometimes you don’t want to argue with unix about whether or not something can be deleted. “Oh boo hoo, this is a non-empty directory.” Too bad, wipe it out!

Terminal window
alias ex-pred='rm -rf'

Pipe stderr to /dev/null§

Do you ever run a command and get a wall of error text that drowns out the actual results you want to see? I find this happens a lot with grep in directories where you don’t have access to all of the files. Take for example this directory where I only have read permission to two folders:

Terminal window
> ls
d-wx--x--x - aaronmorey 14 Jan 13:34 a/
drwxr-xr-x@ - aaronmorey 26 Oct 23:33 b/
d-wx--x--x - aaronmorey 14 Jan 13:34 c/
d-wx--x--x - aaronmorey 26 Oct 23:34 d/
d-wx--x--x - aaronmorey 26 Oct 23:34 e/
drwxr-xr-x@ - aaronmorey 26 Oct 23:34 f/
> grep -inr "a" .
grep: ./a: Permission denied
grep: ./f: Permission denied
grep: ./c: Permission denied
grep: ./d: Permission denied
grep: ./e: Permission denied
./b/5.md:1:# This is a test

I only really care about the line that matches my search. To keep all that other junk out, you could type 2>/dev/null at the end of the line. But I personally find it a struggle to remember that exact sequence. Zsh has a cool feature where you can use -g to expand an alias “globally” anywhere in a line, rather than just at the beginning of a statement (as in bash)

Terminal window
alias -g errtonull='2>/dev/null'

For a usage like this:

Terminal window
> grep -inr --color "a" . errtonull
./b/5.md:1:# This is a test

Scripts§

git_pull_or_clone§

This simple utility takes in a file with one GitHub repository link per line and either clones or pulls from that repo, depending on whether it already exists. I use it to keep plugins in sync.

git_pull_or_clone
#!/usr/bin/env bash
GITFILE=$1
while read g; do
PROJNAME=$g
remove_path="[^\/]+$"
if [[ $PROJNAME =~ $remove_path ]]; then
gitname=${BASH_REMATCH[0]};
dirname=${gitname%.git}
echo $dirname
if [ -d $dirname ]; then
cd $dirname
git pull $g
cd ..
else
git clone $g
fi
fi
done < $GITFILE

gsp§

gsp is short for “git switch and pull”. Somewhat in the same vein as the git_clone_or_pull script above, this takes a git branch name and switches to it if necessary, then pulls the latest from the remote.

Terminal window
#!/usr/bin/env zsh
set -e
branch=$1
git switch $branch
git pull

caffeinate§

I can’t take credit for this one, it’s directly from Apple. But it’s not widely publicized, and it’s come in handy for me a number of times. I periodically need to run a script that takes hours for a single execution. The thing to do in that case is kick it off right before work ends, crack a nice 5:00 beer, and forget about it for a while. But you’ll often check back in later and see that the command failed because your machine went to sleep in the middle. If you use caffeinate, your machine will refuse to sleep until the process completes.

Terminal window
./my_long_process.sh &
ps -ef | grep my_long_process
caffeinate -w <process pid>

Vim§

Format a json file§

Terminal window
nnoremap <localleader>jf :%!jq<cr>

Maps the alias jf (“json format”) to run the command line utility jq command against the current file to format it.