Skip to content

Instantly share code, notes, and snippets.

@imnotashrimp
Last active February 24, 2022 20:18
Show Gist options
  • Save imnotashrimp/5fefe5176071c73079060bc60f0bad4d to your computer and use it in GitHub Desktop.
Save imnotashrimp/5fefe5176071c73079060bc60f0bad4d to your computer and use it in GitHub Desktop.
Shalom's snippets

Shalom's snippets

Just a collection of snippets I use often. Open for the world to see.

Contents

Docker

What it is The command
CLI in the container docker exec -it <CONTAINER> /bin/bash
Stop all containers docker stop $(docker ps -a -q)
Show output to stdout Attach -a to the docker run command

Git

Git aliases

In ~/.gitconfig, add the [alias] section:

[alias]
  recent = log --oneline -5
  undo = !echo "Undoing this commit: " && git log --oneline -1 && git reset --soft HEAD~1 && echo "" && echo "Most recent commits: " && git recent
  bookmark = !git add . && git commit -m '---'
  amend = commit --amend --no-edit
  fileschanged = !read -p 'Starting hash: ' start_hash && git diff --name-only $start_hash
  filesdeleted = !git log --diff-filter=D --summary | grep delete
  trackedbranches = branch -vv

Diverged master

If you committed something you weren't supposed to and borked master, these steps will reset local to origin/master.

Important: You'll lose data, so make sure you know what you're losing before you do this:

git checkout master
git fetch origin
git reset --hard origin/master

Ignore files for all repos on my machine

Make the config change:

git config --global core.excludesfile ~/.gitignore_global

Then add the file you want to ignore to .gitignore_global:

.shalom

Add this file to all your local repos, if you want.

Pruning branches

Prune remote dry run. (To run for real, just remove --dry-run):

git remote prune origin --dry-run

Find local branches whose remote branches were pruned (has to be run in zsh):

git branch -r | awk '{print $1}' | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | awk '{print $1}'

Cool. Now you can delete those branches ☝️ one by one.

GitHub CLI

View PR statuses for each local branch

gh pr list --state all \
--search "$(git for-each-ref --shell --format='%(refname)' refs/heads/ | sed "/'refs\\/heads\\/master'/d" | sed "s_'refs/heads/\(.*\)'_head:\1_" | tr '\n' ' ')"

Google Sheets

Return a null value with a formula

To return a null value with a formula, use IFERROR(0/0).

Javascript

Pretty-print stringified JSON

let json = theJsonYouWantToPrettyPrint;
JSON.stringify(json,null,2);

macOS

Bluetooth not available

If bluetooth crashes for whatever reason and you don't want to restart the machine, you can reset it from the command line:

sudo pkill bluetoothd \
  && sudo rm /Library/Preferences/com.apple.Bluetooth.plist \
  && sudo pkill bluetoothd

Then open Preferences to put everything back in order.

Search from the command line

mdfind -onlyin ./ -name <name>

Enable Touch ID for sudo commands

sudo vi /etc/pam.d/sudo

Add auth sufficient pam_tid.so to the top line so the file looks like this:

# sudo: auth account password session
auth       sufficient     pam_tid.so  # This is the line to add
auth       required       pam_opendirectory.so
account    required       pam_permit.so
password   required       pam_deny.so
session    required       pam_permit.so

In iTerm 2, go to Preferences > Advanced, and type "allow session". Change Allow sessions to survive logging out and back in to No.

Restart the terminal. Test with a simple command like sudo ls.

zsh

Aliases

Add these lines to ~/.zshrc, and then run source ~/.zshrc.

export LESS='-F -i -J -M -R -W -x4 -X -z-4'
alias zshconfig="vi ~/.zshrc && source ~/.zshrc"
alias snippets="open https://gist.github.com/imnotashrimp/5fefe5176071c73079060bc60f0bad4d"

Copy to clipboard

pbcopy < /path/to/file

or

/path/to/file | pbcopy

Terminal colors

Bash colors

New machine setup

@shalper
Copy link

shalper commented Feb 4, 2020

Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment