sdgluck

Fun with Karabiner Elements

Here are some of the ways ways I use Karabiner Elements1 in my development setup.

CMD+1: volume 25%

I am constantly jumping between listening to music, when typically I want the volume to be around 25%, and being in Slack or Zoom calls for meetings and so on, when I want the volume to be 100%.

I got pretty fed up of using the volume controls on my keyboard, whereby I had to hold the volume up/down buttons until I reached the desired volume.

The following Karabiner rule allows pressing CMD+1 for setting the volume to 25%. You should find it trivial to copy and paste it for your needs. I have additional rules for 50% (CMD+2), 75% (CMD+3), and 100% (CMD+4).

{
  "description": "Volume to 25%",
  "manipulators": [
    {
      "type": "basic",
      "from": { "key_code": "f1", "modifiers": { "mandatory": ["left_command"] } },
      "to": [{ "shell_command": "osascript -e 'set volume output volume 25'" }]
    }
  ]
}

CMD+@: git checkout master && git pull

At my current employer we use a monorepo for the bulk of our codebase. I use tmux. I have a git alias git cmap which checks out master and pulls. As handy as that is, I sometimes want to checkout master without focusing my terminal and using my fingers to type, because I am lazy.

I use a rule that makes CMD+@ run git cmap in a window in my main tmux session ("sdgluck") that I pretty much always have open with the current working directory of the aforementioned monorepo ("web").

First, I name my tmux panes after their CWD2:

set-option -g status-interval 5
set-option -g automatic-rename on
set-option -g automatic-rename-format '#{b:pane_current_path}'

Second, I have a shell file at ~/scripts/karabiner.sh which contains the following function:

checkout_master() {
  # get tmux window ID of first window called "web"
  windowId=$(/usr/local/bin/tmux list-windows -F '#I "#W"' awk '$2 ~ /"web"/ { print $1 }')
  # in the first pane of windowId in the sdgluck tmux session:
  #   1. clear the terminal (C-C)
  #   2. type checkout master and pull command ('git cmap')
  #   3. then press Enter
  /usr/local/bin/tmux send-keys -t "sdgluck:$windowId.1" C-c 'git cmap' ENTER
 }

Third, the Karabiner rule:

{
  "description": "dev/web: checkout master",
  "manipulators": [
    {
      "type": "basic",
      "from": { "key_code": "quote", "modifiers": { "mandatory": ["left_command"] } },
      "to": [{ "shell_command": "source ~/scripts/karabiner.sh; checkout_master" }],
      "conditions": [{ "name": "key pressed", "type": "variable_if", "value": 1 }]
    }
  ]
}

References:

  1. https://karabiner-elements.pqrs.org/

  2. https://stackoverflow.com/a/45010147/2039244

#karabiner #mac #osx #tools