[Fsf Programmers] Enhanced 'cd'.

Debarshi 'Rishi' Ray debarshi.ray@[EMAIL-PROTECTED]
Sun May 28 00:17:28 IST 2006


Hi,

This is what I intend to have in GNU Bash:

Suppose the following sequence of comands are executed:
[rishi at Sunflower ~]$ cd /data/
[rishi at Sunflower data]$ cd /usr
[rishi at Sunflower usr]$ cd /usr/local/
[rishi at Sunflower local]$ cd share/
[rishi at Sunflower share]$ cd /bin
[rishi at Sunflower bin]$
Now if some one wants to go back and forth among the list of
directories he has visited, he can choose to use cd -. However cd -
will only take you back and forth between the current ($PWD) and the
last visited ($OLDPWD) directory. ie. /usr/local/share and /bin.

What I am looking to have is something similar to the back and forward
tools in GUI file managers like Nautilus, where something like cd [
will take the user back in the list of directories, instead of merely
entangling him in the present and the past locations visited. eg.,
executing cd [ thrice would take us to /usr, and then executing cd ]
once would take us to /usr/local/. Obviously if the list is empty then
cd [ or cd ] would not change anything.

To implement this we need to have some sort of a stack-like data
structure in my opinion. Whenever someone changes directories into a
new location, the new entry for this location is added to the head of
the stack. Whenever the cd [ command is executed, the head is popped
out and stored somewhere else. Similarly on using cd ] the previous
value popped is restored at the head of the stack and the location
changed. There are two ways of doing this:
1. Modify the builtin cd command.
2. Write a shell function for cd. Probably the use of pushd and popd
can be helpful.

The GNU Bash maintainers have already given their reasons for favouring the
second method, atleast for the time being. I have a written a shell
function for cd, which I have attached to my ~/.bashrc file. I have
use pushd and popd to rotate the directory stack so that whenever a cd
[ command is issued the directory stack is rotated so that the
leftmost entry becomes the rightmost one, and the second entry from
the left becomes the new location. On issuing cd ] the rightmost entry
becomes the leftmost entry and it beomes the new location. Whenever a
cd comand is issued to change into a new directory (eg., cd
/usr/include or cd ..) a new entry is added to the left (head) of the
directory stack. The pushd and popd commands are used for this. The cd
- command has not been satisfactorily modelled till now. The cd -
command should be like alternate cd [ and cd ] commands, and no new
entry should be added to the stack. But as of now, although cd -
enables the user to switch between $PWD and $OLDPWD, it adds a new
entry every time to the directory stack.

However I have some queries regarding the builtin cd command. Why does
it not add the new locations to the directory stack after every change
of location like the pushd command? Moreover it only replaces the left
most entry of the directory stack with the new location encountered.
Are there any specific reasons for doing so?

Your advice and suggestions are eagerly awaited.

Happy hacking,
Debarshi

__COUNT=0
__BACK=0
cd ()
{
	if [ "${1}" = "[" ]; then
		if [ "${__BACK}" -lt "${__COUNT}" ]; then
			pushd +1 > /dev/null
			__BACK=$(($__BACK+1))
		else
			echo "can't go back any more"
		fi
	elif [ "${1}" = "]" ]; then
		if [ "${__BACK}" != 0 ]; then
			pushd -0 > /dev/null
			__BACK=$(($__BACK-1))
		else
			echo "can't go forward any more"
		fi
	elif [ "${1}" = "." ]; then
		builtin cd "${@}"
	else
		if [ "${1}" = "-" ]; then
			pushd "${OLDPWD}" > /dev/null
		elif [ "${#}" -eq 0 ]; then
			pushd "${HOME}" > /dev/null
		else
			pushd "${@}" > /dev/null
		fi
		while [ "${__BACK}" -gt 0 ]; do
			popd -0 > /dev/null
			__BACK=$(($__BACK-1))
		done
		__COUNT=$(($__COUNT+1))
	fi
}



More information about the Fsf-prog mailing list