Syncing zsh named directories

One of my most used features of zsh is named directories. I keep my dotfiles synced across multiple machines via Dropbox (referral link) and a Makefile that creates symlinks automagically.

nn

My named directories start with ~ for my home directory so they need to exist for expansion to work properly and not output errors when I log in. When I first implemented syncing I added this check with a very simple test:

nnnnnnn

1n
[[ -d ~/Dropbox/wiki           ]] && wiki=~/Dropbox/wikin

nnn

One line like this isn’t too bad but when you have 30, it gets a little silly looking.

nn

I finally took the time to sit down and read the docs on zsh associative arrays and whipped up a much better solution.

nn

1n2n3n4n5n6n7n8n9n10n11n12n13n14n15n16n17n18n19n
typeset -A NAMED_DIRSnnNAMED_DIRS=(n    bin      ~/Dropbox/Documents/binn    docs     ~/Dropbox/Documentsn    lbin     ~/Documents/binn    wiki     ~/Dropbox/wikin    dot      ~/Dropbox/Documents/dotfilesn    octo     ~/Dropbox/Documents/octopressn)nnfor key in ${(k)NAMED_DIRS}ndon    if [[ -d ${NAMED_DIRS[$key]} ]]; thenn        export $key=${NAMED_DIRS[$key]}n    elsen        unset "NAMED_DIRS[$key]"n    findonen

nnn

The unset isn’t necessary here for the base functionality. I had another problem. With 30 or 40 named dirs, some belonging to projects that I might not have looked at in 6 months and suddenly need to come back to, it’s hard to remember the name I gave for a directory. I added an lsdirs function that lists just the named directories that existing on the machine I’m on.

nn

1n2n3n4n5n6n
function lsdirs () {n    for key in ${(k)NAMED_DIRS}n    don        printf "{7e898290d39a0d5231795fca98f9d583658ec52d7dd28242e01977702bb4ac4e}-10s {7e898290d39a0d5231795fca98f9d583658ec52d7dd28242e01977702bb4ac4e}sn" $key  ${NAMED_DIRS[$key]}n    donen}n

nnn

This gives me nice easy to read output with both the names and the full path.

nn

This blog post is more future reference for myself but perhaps it may be useful to you.

nn

For those interested, here is the Makefile I use:

nn

(Makefile) download
n

1n2n3n4n5n6n7n8n9n10n11n12n13n14n15n16n17n18n19n20n21n22n23n24n25n26n27n28n29n30n31n32n
DOTFILES ?= $(shell pwd)nHOSTNAME ?= $(shell hostname)nnall: shell code perl web mailnnshell:n  ln -fs $(DOTFILES)/zsh/zshrc        ${HOME}/.zshrcn  ln -fns $(DOTFILES)/zsh/zsh.d       ${HOME}/.zsh.dn  ln -fs $(DOTFILES)/gnupg            ${HOME}/.gnupgn  ln -fs $(DOTFILES)/ssh/config       ${HOME}/.ssh/confignn    ifeq ($(HOSTNAME),orion)n      ln -fs $(DOTFILES)/screen/screenrc_orion ${HOME}/.screenrcn    elsen      ln -fs $(DOTFILES)/screen/screenrc  ${HOME}/.screenrcn    endifnncode:n  ln -fs $(DOTFILES)/gitconfig        ${HOME}/.gitconfign  ln -fs $(DOTFILES)/vimrc            ${HOME}/.vimrcn  ln -fns $(DOTFILES)/vim             ${HOME}/.vimn  ln -fs $(DOTFILES)/.tm_properties  ${HOME}/.tm_propertiesnnperl:n  ln -fs $(DOTFILES)/perltidyrc       ${HOME}/.perltidyrcn  ln -fs $(DOTFILES)/perlcritic       ${HOME}/.perlcriticrcnnweb:n  ln -fs $(DOTFILES)/vimperatorrc   ${HOME}/.vimperatorrcnnmail:n  ln -fs $(DOTFILES)/muttrc           ${HOME}/.muttrcn

nn