Today I Learned

A Zero One initiative

6 posts about #bash

Configure Tmux to use 1 as its first window index

Tmux will start its first window at index 0, which can be unintiutive when you visually associate your windows with the number row on your keyboard. i.e. your first Tmux window is the last number on the number row (0), and the second window is the first number on the row (1).

This config in ~/.tmux.conf will make 1 the starting index so that Tmux windows and the number row both grow from left to right in an intuitive manner.

# Start windows and panes at 1, not 0
set  -g base-index      1
setw -g pane-base-index 1

Prevent commits to main/master with git hooks!

You can use git hooks to keep you safe when you aren’t thinking properly and decide to commit something to main/master.

It would be nice if we could create a hook which would prevent commits to main/master unless we give it some override. Like a crimesjohnson prefix.

As a start I decided to make a global hook for all of my projects with this command:

git config --global core.hooksPath ~/githooks

I then created a file (within ~/githooks) for a special kind of git hook and named it commit-msg.

#!/bin/sh

commit_msg=$(cat "${1:?Missing commit message file}")
branch="$(git rev-parse --abbrev-ref HEAD)"

if ([ "$branch" = "main" ] || [ "$branch" = "master" ]) && [[ "$commit_msg" != *"crimesjohnson:"* ]]; then
  echo "You can't commit directly to main/master branch"
  exit 1
fi

Don’t forget to make it executable: chmod +x ~/githooks/commit-msg

When you create a commit message, it seems like the message is saved to a temp file while the commit-msg hook runs. We get the commit message by cating it.

We also use a git command to get the name of our branch.

Using these 2 bits of info, we can then write a simple if statement to keep us safe!

How to use TouchID for sudo access

If you’re on a Mac with TouchID, you can easily configure it to prompt for a TouchID scan alongside normal password auth when running a command via sudo.

  • Edit /etc/pam.d/sudo with sudo
  • Add this as the first line underneath the comment at the top:
auth sufficient pam_tid.so
  • Save the file, and the next time you require sudo, you should be greeted with a TouchID prompt.

Notes

  • I haven’t tested this on the new magic keyboards with integrated TouchID, but there’s no reason to believe it won’t work with these keyboards too.
  • You may need to reapply this config when MacOS updates

Environment variables in crontabs

If you need to set an env var that is used for a bunch of cron jobs, you can set it once at the top of your crontab, and cron will be smart enough to make it available to all jobs specified.

PATH=/my/extra/paths/bin:$PATH

# MIN HOUR DOM MONTH DOW CMD Do something in a strange path
0     12   *   *     *   run_this_command_that_lives_somewhere_strange -rf *

Easily reuse commands in your shell

Two techniques I love using when working with commands in the shell are quickly running a previous command in my history, and rerunning a previous command, but with sudo.

To quickly rerun a command in your history, simply run !<history_number>, e.g.

$ history

 1451  git pull
 1452  git diff docker-compose.yml
 1453  git checkout -- docker-compose.yml
 1454  vim docker-compose.yml
 1455  docker-compose down
 
$ !1454 # reruns vim docker-compose.yml

Next, I often run a command as a user, when I meant to run it as root, !! is a shell expression which expands to the last command. e.g.

$ service docker restart

Failed to restart docker.service: Insufficient privileges

$ sudo !! # will expand to sudo service docker restart