diff --git a/functions/misc.zsh b/functions/misc.zsh index 426b34d..eef0459 100755 --- a/functions/misc.zsh +++ b/functions/misc.zsh @@ -284,3 +284,19 @@ function mangrep() { MANPAGER="less -p \"$pattern\"" man "$cmd" } + + +# watches the given file and executes the given action whenever the file is changed. Usage: watch_file +function watch_file() { + if ! which inotifywait &>/dev/null ; then echo "$0 requires inotifywait"; return 1; fi + [[ -z "$1" ]] && "Usage: $0 " && return 1 + local file + file="$1" + shift + + [[ -z "$@" ]] && "Usage: $0 " && return 1 + + local action + action="$@" + while inotifywait -e close_write "$file"; do zsh -c "$action"; done +}