Friday, November 13, 2009

Last Directory Visited

From the shell, typing "cd -" will take you to the last directory visited. This is useful from a command-line navigation perspective and from inside of shell scripts as well.


Philipp said...

Of course, this works in the VIM command-line as well:

:cd /tmp
:cd -

dominiko said...

[...] and from inside of shell scripts as well.

In shell script, I find it cleaner to do cd in a subshell so that the old dir is automatically restored.

# commands in olddir
( cd newdir
# commands in newdir
# commands in olddir

egypt said...

Don't forget about pushd and popd. pushd works just like cd but puts your current directory onto a stack. popd removes the directory from the top of the stack and cds into it.

Tobias said...

This was a pretty mundane bash tip, and not really a vim tip ... But pushd and popd were new to me! Thanks!

Travis Whitton said...


Yeah, sorry, they can't all be exciting.

pushd and popd were covered in a previous post: