# source: https://github.com/ansemjo/dotfiles/blob/debfb84d2574985daeac0d85a66002081b59a569/fish/functions/tmpdir.fish function tmp --description "Create and switch into an emphemeral directory" # create a tmpdir and cd into it set -l tmpdir (mktemp --tmpdir -d tmpdir-XXXXXX) cd $tmpdir; and echo >&2 \ (set_color yellow)"tmpdir:" $tmpdir "will be removed on exit" \ (set_color normal) # spawn a new shell, store the exit status and return to previous dir fish $argv set -l ret $status cd $dirprev[-1] # after exit, check if there are mounts inside tmpdir if test -e /etc/mtab && awk '{ print $2 }' /etc/mtab | grep $tmpdir echo >&2 \ (set_color red)"refusing to purge $tmpdir due to mounts!" \ (set_color normal) else # if clear, purge directory echo >&2 \ (set_color red)"purge $tmpdir ..." \ (set_color normal) rm -rf $tmpdir end # return subshell exit status return $ret end