Getting nice bash aliases with Ruby

in ruby

I like shortcuts a lot. I like aliases and I like all the stuff that makes me more productive. Even if we are talking about a couple of characters to change a directory in the terminal. So I am one of those people that stores all their code in one directory, precisely in $HOME/code. And I found it a bit annoying to type something like:

cd[ENTER]
cd c[TAB] --> cd code[ENTER]
cd my-awesome-project[ENTER]

when I wanted to reach a project of mine. It was annoying because I had to do it many times in a day even to reach the same directory. So I though it would be nice to get some dynamic aliases to reach my projects with something like the following:

my-awe[TAB] --> my-awesome-project[ENTER]

It would have been a small boost in my productivity. So I tried to do it with Bash scripting but, since I am not a bash hero, I wasn’t successful. So I switched to Ruby and I came up with the following script:

#!/usr/bin/ruby 

Dir["#{File.expand_path("~/code")}/*"].each { |file|
    if File.directory?(file)
        cmd=File.basename(file)
        puts "alias #{cmd}=\"cd #{file}\"" if `which #{cmd}`.length == 0
    end
}

To my mind it is quite self-explanatory. And in my bashrc I put the following:

$HOME/.bash/code_dirs.rb > $HOME/.bash/codedirs
source $HOME/.bash/codedirs

This simply redirects the output of the above-mentioned Ruby script in a file sourced in the current bash shell. It just works fine for me.

Fork me on GitHub