zsh

Add jog (previous commands in the directory) functionality to zsh with zsh-histdb

 jog() {
      sqlite3 $HOME/.histdb/zsh-history.db "
  SELECT
      replace(commands.argv, '
  ', '
  ')
  FROM commands
  JOIN history ON history.command_id = commands.id
  JOIN places ON history.place_id = places.id
  AND dir = '${PWD}'
  AND places.host = '${HOST}'
  AND commands.argv != 'jog'
  AND commands.argv NOT LIKE 'z %'
  AND commands.argv NOT LIKE 'cd %'
  AND commands.argv != '..'
  ORDER BY start_time DESC
  LIMIT 10
  "
}

Source

execute common commands in directory

 com() {
    eval $(sqlite3 $HOME/.histdb/zsh-history.db "
    SELECT
        replace(commands.argv, '
    ', '
    ')
    FROM commands
    JOIN history ON history.command_id = commands.id
    JOIN places ON history.place_id = places.id
    AND dir = '${PWD}'
    AND places.host = '${HOST}'
    AND commands.argv != 'jog'
    AND commands.argv NOT LIKE 'z %'
    AND commands.argv NOT LIKE 'cd %'
    AND commands.argv != '..'
    GROUP BY commands.id
    ORDER BY COUNT(commands.id) DESC
    LIMIT 10
    " | fzf)
  }

Add functions directory to zsh

In ~/.zshrc

fpath=( "$DOTFILES_DIR/zsh/functions" "${fpath[@]}" )
autoload -U $fpath[1]/*(.:t)

Scripting

Arrays

Concatenating Arrays1

a=(1 2 3)
b=(4 5 6)
c=($a $b)
echo $c
# => (1 2 3 4 5 6)

Diffing Arrays2

Subtraction

a=(1 2 3)
b=(1 2)
c=${a:|b}
echo $c
# => (3)

Intersection

a=(1 2 3)
b=(1 2)
c=${a:*b}
echo $c
# => (1 2)

Add keyboard shortcuts

bindkey -s '^f' 'git show\n'

The above would run git show whenever Ctrl+f is pressed. The flag -s stands for substitute, i.e. substitute Ctrl+f for git show and carriage return to execute \n.

References

1.
Array concatenation - Rosetta Code. at https://rosettacode.org/wiki/Array_concatenation#Zsh.
2.
Zsh Development Guide (Part 5 Array). at https://programmer.help/blogs/zsh-development-guide-part-5-array.html (2019).

Startup order

There are system wide configs (/etc) and user specific (home directory):

file when
/etc/zshenv always
~/.zshenv usually
/etc/zprofile login
~/.zprofile login
/etc/zshrc interactive
.zshrc interactive
/etc/zlogin login

PATH and macOS load order

macOS has a path helper (/usr/libexec/path_helper) that is run in etc/zprofile which will modify PATH setup up that point. This means that =~.zshenv= may end up clobbered. The recommended cure is to place PATH modifications in ~/.zshrc. Unfortunately this runs counter to other advice you may receive. For example, at one point spacemacs asked for PATH configuration in ~/.zshenv. Ultimately you may need to manage some path work in both places depending on the problem being solved, and /etc/paths(.d) is an alternative (but apparently macOS specific).

Links to this note