3 # Description: Autodetects a nnn remote mountpoint (mounted with `c`)
4 # from any of its subfolders and allows unmounting it
5 # from the subdir without navigating to the mountppoint
6 # or entering the remote name. Also works when hovering
7 # the mountpoint directly like vanilla `u`.
9 # Dependencies: fusermount
11 # Shell: POSIX compliant
12 # Authors: Kabouik & 0xACE
15 # - Avoid lazy unmount by forcing nnn context to leave the subfolder before fusermount.
16 # Tried `printf "%s" "0c$m" > "$NNN_PIPE"` but it breaks the nnn interface, see #854.
19 m=$HOME/.config/nnn/mounts
20 if [ "$PWD" = "$m" ]; then
21 # Allow running the script on hovered directory if user is in ~/.config/nnn/mounts
24 d=$(dirname "$(readlink -f "$1")" | grep -oP "^$m\K.*" | cut -d"/" -f2)
27 # Test if user is within $m or a subdir, abort if not
28 if [ "$d" = "" ]; then
29 clear && printf "You are not in a remote folder mounted with nnn. Press return to continue. " && read -r _
31 # Test if $m/$d is a mountpoint and try unmounting if it is
32 mountpoint -q -- "$m/$d"
33 if [ "$?" -eq "1" ]; then
34 clear && printf "Parent '%s' is not a mountpoint. Press return to continue. " "$d" && read -r _
36 cd "$m" && fusermount -uq "$m/$d" || err=1
37 if [ "$err" -eq "0" ]; then
38 rmdir "$m/$d" && clear && printf "Parent '%s' unmounted." "$d"
40 clear && printf "Failed to unmount. Try lazy unmount? [Yy/Nn] " && read -r
45 # If unmount fails, offer lazy unmount
46 if [ "$REPLY" = "y" ] || [ "$REPLY" = "Y" ]; then
48 cd "$m" && fusermount -uqz "$m/$d" || err=1
49 if [ "$err" -eq "0" ]; then
50 rmdir "$m/$d" && clear && printf "Parent '%s' unmounted with lazy unmount. " "$d"