DEV Community

Discussion on: Bash: How To Teleport In The Terminal

Collapse
 
andyanderson profile image
Andy Anderson

Great idea, Jimmy! I’ve used aliases with pushd/popd for decades, actually, but never thought to include an automatic ls on the end. For example:

alias ,=pushd

This uses one other nice feature of pushd — by itself it swaps the top two items on the directory stack! So you can quickly go back and forth between two directories with a single comma.

Including && ls at the end of this, though, prevents applying it in general, as any trailing file/directory arguments are picked up by the ls. But if you instead use a shell function:

function , { pushd $1 && clear && ls; }

it works both with and without an argument.

Also, if you don’t mind occasionally typing out source, you can repurpose . :

alias .=popd

I’m using . here because it’s next to ,, it signals finality, and there really aren’t any other punctuation keys available without shift that aren’t interpreted by the shell for something else 😀

I really like the .. idea, though I doubt I would use more than one level very often. One other suggestion I would make is to perhaps provide a variant,

alias ,,="pushd .. && clear && ls"

to add the parent directory to the directory stack.

Finally, sometimes I want to remind myself of the current directory stack, which is displayed with dirs. Again, being short on punctuation keys but given this is not that common, I can alias it to ,., which is in keeping with the rest of these options:

alias ,.=dirs