Custom Bash completions for lazy bastards

by on Jan.25, 2010, under Linux

At $work, we have a log server, where the current day’s logs from a given machine are stored in, e.g. /logs/machinename/year/month/day/. In a moment of yak shaving, I added a function to my .profile, named cdmachinelogs to allow me to type e.g. cdmachinelogs somemachine, and end up in the appropriate directory.

Now, Bash will automatically tab-complete e.g. “cdm” into cdmachinelogs, but I wanted to be extra-lazy and tab-complete the machine name, too.

Enter the following snippet:

complete -W "$(find /log/path -maxdepth 1 -mindepth 1 -type d -printf '%f ')" cdmachinelogs

So, it sets up tab-completion for the cdmachinelogs command, using a list of words obtained by finding each directory name under the path to the logs.

Posted mostly for my own later reference, but also for anyone who might find it useful.

2 Comments for this entry

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!