A better console with autols
Some years ago (on November 10th, 2006) I wrote one of my favourite console hacks.
At that time I was digging in Bash and discovered the PROMPT_COMMAND variable. It defines a command line that will be run before the prompt is showed. I thought this might be useful to see the files in a directory when enters in it, so the cd → ls sequence will not be so repeated. There are some constraints:
It has to be fast. I use the console a lot and don't like waiting to the prompt to be ready.
The content should be limited. If a directory has hundreds or thousands of files it should not list them.
I would like to have a little header with a summary of the content (total size, number of directories, etc).
After some minutes I had coded a dirty C program which makes everything, with a little Bash support. Something like this:
I've been really enjoying this for years, and some weeks ago I realized that almost nobody knows this hack. Last friday, in a technical meeting at Aentos, I decided to release this to anyone who is interested. I refactored the code (mostly in order to have arguments, since I had harcoded my own options) and publish this at GitHub.
The project is available at https://github.com/ayosec/autols. Enjoy it!
How to detect the change directory.
An insteresting part of this hack is how to detect when the prompt has gone to a different directory. In Zsh it is very easy, since it provides a chpwd function. With the add-zsh-hook function the code is even easier.
autoload add-zsh-hook launch_autols() { eval autols $AUTOLS_OPTIONS } add-zsh-hook chpwd launch_autols
Bash has nothing like a change directory hook. RVM replaces the cd with a custom function to detect the directory changes. This hack is not useful for me since I use many ways to change directory. Concretly, autocd, pushd/popd, the awesome cdargs tool and even the CDPATH variable (yes, I use all of them). None of these methods will invoke the cd function. The best way (although not the nicest) to detect a new directory is to check the current directory every time the prompt is showed.
The result is a little uglier than the Zsh equivalent, but it works well.
function launch_autols { if [ ! x$AUTOLS_LAST_DIR = x$PWD ] then AUTOLS_LAST_DIR=$PWD eval autols $AUTOLS_OPTIONS fi } AUTOLS_LAST_DIR=$PWD PROMPT_COMMAND="launch_autols;$PROMPT_COMMAND"












