This is a collection of random technical notes that I’ve accumulated over time.

Git

# enable repo wide commit signing
git config commit.gpgsign true

# set global signing key
git config --global user.signingkey CAFEBABE

# create new branch, take local changes along
git checkout -b new-branch

# remove local branch
git branch -d new-branch

# add single hunk from a file
git add -p

# push the current branch
git push origin HEAD

# create alias "git new" to init with empty commit
git config --global alias.new '!git init && git commit --allow-empty -m "initial commit"'

# update all submodules
git submodule foreach git pull origin master

# create and apply patches
git format-patch -1 <commit-sha>
git am <patch-file>

xcrun: error

If running into

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

while doing e.g. git status (especially after a MacOS update), re-run xcode-select --install or sudo xcode-select --reset.

Emacs

Splitting windows

  • C-x 2: split into two windows, one above the other
  • C-x 3: split into two windows, side by side
  • C-x o: switch to other buffer in window split

(Spacemacs only) Required package request-0.1.0 is unavailable

see issue #4642

  1. M-x list-packages
  2. search and install request
  3. restart emacs M-m f e R (for ‘evil’: SPC f e R)

Duplicate current line

(defun duplicate-line()
  (interactive)
  (move-beginning-of-line 1)
  (kill-line)
  (yank)
  (open-line 1)
  (next-line 1)
  (yank)
  )

(global-set-key (kbd "C-c d") 'duplicate-line)

Delete entire current line

(global-set-key (kbd "C-c f") 'kill-whole-line)

Mac command key work as expected again (Mac)

(setq mac-option-modifier 'nil)
(setq mac-right-option-modifier 'nil)
(mac-auto-operator-composition-mode)

Webserver configuration

Enable autorun on boot (Ubuntu)

sudo update-rc.d supervisor defaults

Objective C

Blocks

// define:
typedef retTy (^BlockName)(int foo, NSString *bar);

// use:
BlockName block = ^retTy(int foo, NSString *bar) {
    // do stuff
};

// as property:
@property (nonatomic, copy) BlockName myBlock;

JavaScript / TypeScript

Install on Mac

Use nvm to install and manage node & npm versions.

ESLint and Prettier

npx eslint --init
npm i --save-dev prettier eslint-config-prettier eslint-plugin-prettier

Next, add prettier to extends and plugins in the eslint config. Also, add a rule that prettier rules are reported as errors:

{
  "extends": [
    "prettier",
  ],
  "plugins": [
    "prettier"
  ],
  "rules": {
    "prettier/prettier": "error"
  }
}

Tweak the prettier config to your likings:

module.exports = {
  trailingComma: 'es5',
  semi: true,
  tabWidth: 2,
  singleQuote: true,
}

If you are using VS Code, create a file .vscode/settings.json with this content:

{
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
}

Potential eslint rules to disable for use with TypeScript

{
  rules: {
    'react/react-in-jsx-scope': 'off',
    '@typescript-eslint/no-explicit-any': 'off',
  },
}

Benchmarks

Docker

Configure daemon to listen on TCP

sudo emacs /lib/systemd/system/docker.service # remove -H option
sudo emacs /etc/docker/daemon.json # update to the following:
# { "hosts": [ "tcp://127.0.0.1:2375", "fd://" ] }
sudo systemctl daemon-reload
sudo systemctl restart docker.service

Homebrew

Run on new Apple M1 ARM chip

# install via
arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

# usage example
arch -x86_64 brew install rocksdb

Bash

Load environment from .env file

export $(cat .env | xargs) && my-program

Get scripts directory

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

Sane shell script

#!/bin/bash -eo pipefail

Tools

Clipboard syntax highlighting (MacOS)

#!/bin/bash -eo pipefail

pbpaste | pygmentize -l $1 -f rtf | pbcopy -Prefer rtf

Usage: syntax.sh python

AWS