dylanaraps/shfm

ability to use ~ with cd feature

CorruptCommit opened this issue · 3 comments

I got toying with a pull request to allow the use of ~ with the cd feature. Let's say you are deep within the file system and you want to move to your Downloads directory. It would be much nicer to just type : ~/Downloads than : /home/user/Downloads or go up like a dozen directories to get $HOME.

This is what I have got so far:

@@ -321,7 +321,11 @@
 
             :?)
                 prompt "cd: " r
-                cd "${ans:="$0"}" >/dev/null 2>&1|| continue
+		         path=${ans:="$0"}
+		         case $path in "~"*)
+		             path=${path/\~/$HOME}
+		         esac
+                cd "$path" >/dev/null 2>&1|| continue
                 set -- *
                 y=1 y2=1 cur=$1
                 redraw "$@"

This is not pure Posix and shellcheck errors out with the following:

$ shellcheck shfm

In shfm line 326:
		    path=${path/\~/$HOME}
                         ^--------------^ SC2039: In POSIX sh, string replacement is undefined.

For more information:
  https://www.shellcheck.net/wiki/SC2039 -- In POSIX sh, string replacement i...

While this works for me, it does not fit within the goals of this project. I am also concerned it may not work on other systems.

At this point, I am a bit stumped and not sure if this functionality is possible within pure Posix. Posix is a bit out of my wheelhouse so any suggestions would be welcomed.

Or maybe allow the cd function to allow access the file structure under $PWD? Like the $PWD is $HOME/.local, and I can just type : and bin/dir to go to $HOME/.local/bin/dir. Would this be possible?

Hey @huijunchen9260,

Thanks for sharing. The behaviour you described already works and even works outside of $HOME.

@huijunchen9260 : and bin/dir already works everywhere. Both relative and absolute paths can be used.

~ works now too.