DevTips -- CLI Tools and Shell Scripts

moon indicating dark mode
sun indicating light mode
Command Line Interfaces (or CLIs) range from single-purpose tools and builtins like ls, cd, and cat to full fledged programs and text user interfaces like yarn, midnight commander, or your own scripts.

comm

comm is a utility that, given two files, can output 3 columns. Lines in a, lines in b, and lines in both. Output can be restricted using -1, -2, or -3 flags.

comm a.txt b.txt
a
b
c
d
f
// file a contents
a
b
d
f
// file b contents
a
b
c

bat replaces cat

bat (https://github.com/sharkdp/bat) is a cat replacement that includes syntax highlighting, line numbers, and more.

───────┬────────────────────────────────────────────────────────────
│ File: /Users/chris/.zshrc
───────┼────────────────────────────────────────────────────────────
1 │ ZSH=$HOME/.oh-my-zsh
2 │
3 │ # Source private aliases, etc that don't get checked in
4 │ source ~/.zshrc-priv
5 │
6 │ # Vars
7 │ export ALTERNATE_EDITOR=""
8 │ export EDITOR=emacsclient
9 │ export PUBLISH=$DROPBOX/publishing
10 │ export ORG=$DROPBOX/__notes/_org
11 │ export GITHUB=$HOME/github

Exa can replace ls

Exa (https://the.exa.website/) is a modern replacement for ls written in Rust. You can alias it in your init scripts.

alias ls = exa
➜ exa
content go.sum netlify.toml packages www
go-src Makefile node_modules README.md yarn.lock
go.mod netlify-functions package.json sens8

direnv

direnv (https://direnv.net/) is a utility that will look for a `.envrc` file and put the exported environment variables into your shell environment.

# Create a new .envrc. This file is bash code that is going to be loaded by
# direnv.
echo export ACCESS_TOKEN=something > myproject/.envrc
cd myproject
# direnv: loading .envrc
cd ..
# direnv: unloading

git status -sb

git status -sb will produce more compact output, showing modified (M), deleted (D), and untracked files (??)

➜ git status
On branch gatsby-2
Your branch is up to date with 'origin/gatsby-2'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: go-src/devtips/main.go
modified: go.mod
modified: go.sum
deleted: www/content/notes/rocket-league-replay-format.mdx
Untracked files:
(use "git add <file>..." to include in what will be committed)
go-src/rpc-logger.go
www/content/dev-tips/cli.yaml
www/content/dev-tips/cli/
www/content/notes/node/
www/content/notes/rocket-league/
www/content/posts/removing-drafts-from-gatsby-theme-blog.mdx
no changes added to commit (use "git add" and/or "git commit -a")
➜ git status -sb
## gatsby-2...origin/gatsby-2
M go-src/devtips/main.go
M go.mod
M go.sum
D www/content/notes/rocket-league-replay-format.mdx
?? go-src/rpc-logger.go
?? www/content/dev-tips/cli.yaml
?? www/content/dev-tips/cli/
?? www/content/notes/node/
?? www/content/notes/rocket-league/
?? www/content/posts/removing-drafts-from-gatsby-theme-blog.mdx

yarn workspace alias

Tired of typing out `yarn workspace <workspace name> <script>`? Try out this alias that uses jq and fzf to let you choose a workspace from a list. `yw develop`

yw() {
yarn workspace $(yarn workspaces info --json | jq '.data' -r | jq "[keys][0] []" -r | fzf) $@
}