Bash and Unix tools

May 29, 2017

After a few introductory guides, improving your command line fu just comes down to seeing what other developers use. Here are some less common tools I’ve found invaluable enough to use on every system.

For reference, I keep my dotfiles on Github.

.inputrc

Readline (the GNU library for reading user input that Bash and others use) has it’s own dotfile: ~/.inputrc. A sample of the life changers (mostly just saner defaults) in my own inputrc:

Search history with Up/Down arrow keys.

"\e[A": history-search-backward
"\e[B": history-search-forward

By default, Bash steps through your entire history when using the arrow keys. With this setting, you only step through commands that start with what you’ve already typed. E.g. if you’ve typed git and then hit up, it only steps through commands starting with git, like git status, git add .. This is basically a more convenient Ctrl-R for recursive history search.

Ignore case for tab completion

set completion-ignore-case on

Example: ~/docum <Tab> will autofill ~/Documents/

Show all suggestions after pressing tab once instead of twice

set show-all-if-ambiguous on

Example: ~/d <Tab> immediately shows the list of possible completions (~/Desktop, ~/Documents), rather than waiting for you to mash tab twice like a buffoon.

SSH Escape Sequences: ~.

<ENTER>~. kills an SSH session. Comes in handy when you wake up your computer to find an unresponsive SSH session. (Enter is not required, but typically I find I need it). You can see more escape sequences with <ENTER>~?.

Syntax highlighted less and cat

You can use pygments to give less syntax highlighting when viewing files:

# In my .bashrc
export LESSOPEN="|<path/to/lesspipe.sh> %s"
alias less='less -R'

Where <path/to/lesspipe.sh> points to this script..

Also just running pygmentize works as a colorized cat.

OSX: pbpaste and pbcopy

On OSX, pbpaste and pbcopy (which I have aliased to pp and pc) give you access to the clipboad, making it incredibly convenient to run commands to modify the contents of your clipboard like pp | tr -s ' ' | pc.

Other small things

Beyond that, the smaller things add up:

# Get running processes |
#  filter by search |
#  remove extra spaces |
#  select column with pid |
#  pass to kill
ps aux | \
  grep -i 'my-process' | \
  tr -s ' ' | \
  cut -d ' ' -f 2 | \
  xargs kill

> echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool
{
    "bar": "ipsum",
    "foo": "lorem"
}
sorted-du () {
  paste -d '#' <(du -cs *) <(du -chs *) | sort -rn | cut -d '#' -f 2
}
> history | tr -s ' ' | cut -d' ' -f3 | sort | uniq -c | sort -rn
1195 la
 988 cd
 542 make
 536 todo
...