Sam's dotfiles highlights
Tags: bash
Published: 2019-06-06 05:17:51

---
title: Sam's dotfiles highlights
published: true
description: A brief insight into my dotfiles and helpers
tags: bash
canonical_url: https://whistlr.info/2019/dotfiles-highlights/
series: Blog-A-Day in June
---

Most developers end up creating some sort of [dotfiles repo](https://www.google.com/search?q=dotfiles+repo) to contain configuration files for their *nix-like machines (macOS, Linux, etc). Rather than just setting up every machine you access in an ad-hoc way, you can create a repo to keep the same environment for your terminal everywhere.

There's a lot of starting points out there that you could clone from. If you know `git`, my advice would be to follow [how to manage them with `git`](https://www.anand-iyer.com/blog/2018/a-simpler-way-to-manage-your-dotfiles.html)—this guide lets you use a special `dotfiles` command.

My dotfiles are pretty boring, but I think there's a few parts that make mine special. Let's talk through them. I use `bash`, the default in most places.

1. Layout—_what calls what and how things are run_
2. Platform Help—_distinguish macOS and Linux_
3. Helpers—_take a look inside my scripts_

## Layout

My layout is pretty simple. The `.bash_profile` file immediately runs `.bashrc`, so there's no difference in how you log in (the two files being different is a big source of confusion for newbies—so just merge them! 🤣). 

![Bash layout](https://thepracticaldev.s3.amazonaws.com/i/iccp5s95rycnm48jvhew.png)

One file that I've used for years is [`.bash_functions`](https://gist.github.com/samthor/23d5f0a6e3a94c2f05310a197299a78d) (originally written by a friend back in ~2009). It provides a few helpers that make dealing with paths "safe". Here's a simple example—both `pathprepend` and `ssource` check that the path or file exists before using it:

```bash
#!/bin/bash

source "${HOME}/.bash_functions"  # first line of .bash_profile

# adds to $PATH if the directory exists
pathprepend "/opt/local/sbin"
pathprepend "/opt/local/bin"
pathprepend "${HOME}/.local/bin"

# runs contents of .bash_local if it exists (not checked in, for
# custom changes on each machine)
ssource "${HOME}/.bash_local"
```

### A Note On SSH

🚨 While I do check in my `id_rsa.pub` file to every machine—this is the _public_, sharable part of your identity—I don't include `id_rsa` in my dotfiles repo. I keep it _only_ on machines I have physical access to, such as my laptop or a server in my house.

This is important. Your `id_rsa` (or `id_dsa`?) is private and even if it has a password, is going to be cracked if you accidentally upload it somewhere public.

## Platform Help

At the top of my `.bashrc`, I set a handy variable:

```bash
IS_MAC=""
UNAME=$(uname)
if [ "${UNAME}" == "Darwin" ]; then
  IS_MAC="yes"
fi
```

I switch between macOS (which is really BSD and has older versions of things) and Linux, so this helps make a choice if things work differently, as it does for the params to `ls`:

```bash
if [ $IS_MAC ]; then
  alias ls='ls -lGho'  # macOS doesn't support `--color=auto`, needs `-G`
else
  alias ls='ls -lh --color=auto'  # on Linux, `-G` means "no groups"?!
fi
```

## Helpers

I've got a few scripts inside my `~/.local/bin` folder. Some of which I use almost daily. 📜❗#️⃣

### Image Compression

I have two scripts—[crush and pcrush](https://gist.github.com/samthor/f19290225a763c2df13bd48f1915dee8)—which compress a single PNG image, or compress many in parallel. I never remember the arguments to run the tools `pngcrush` or `zopflipng` (better), but it's always worth shrinking your images before you upload them (gotta go fast!).

I built `pcrush` so I could also take advantage of multiple CPUs—turns out both the tools above run on a single CPU only, but if I have 100s of images, I want to crush them fast. I can make my CPUs do this:

![parallel compression](https://thepracticaldev.s3.amazonaws.com/i/m66xzvouykuefty1vsxp.png)

### Git Status

I have a script called `gstate` which outputs a number of things based on the current Git repository. It includes branches, normal status, any stashed changes, and known divergence from upstream (i.e., the remote repo you've mirrored from).

It's handy and honestly burned into my muscle memory forever now. 💪

{% twitter 1120991208782778368 %}

If you're curious, [check it out here](https://gist.github.com/samthor/c333e87f70631ecee50cd46f00e175cd).

## That's All!

I hope today's short view into my environment has been interesting! Got your own handy scripts you can't live without? Let me know! 🗣️💬

<small>(This Blog-A-Day post goes out to my partner: holding a screaming baby while I write.)</small>

6 👋