Skip to content

Instantly share code, notes, and snippets.

@mislav
Last active July 24, 2017 22:32
Show Gist options
  • Save mislav/a4cbfe173c9ed835b900c264a247e1dc to your computer and use it in GitHub Desktop.
Save mislav/a4cbfe173c9ed835b900c264a247e1dc to your computer and use it in GitHub Desktop.
Open specific editor based on directory contents
export EDITOR_XCODEPROJ='open -a XCode'
export EDITOR_TSCONFIG='code'
export EDITOR_DEFAULT='atom'
export EDITOR='magic-open'
#!/bin/bash
set -e
target="${1?}"
if [ -d "$target" ]; then
dir_contents="$(ls -a "$target")"
for editor_type in $(env | grep ^EDITOR_ | cut -d= -f1); do
matcher="${editor_type#EDITOR_}"
if [ "$matcher" != 'DEFAULT' ] && grep -iq "$matcher" <<<"$dir_contents"; then
exec ${!editor_type} "$target"
fi
done
fi
exec ${EDITOR_DEFAULT?} "$target"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment