Customizing bashrc, bash aliases, and more – I

If you are used to work with the bash console, probably you have noticed that many tasks/commands are repeated very often. In order to improve the speed/productivity, I’m always customizing bashrc files, bash aliases, and creating helper commands whenever it is possible.

For instance, if you work in many different web projects, and you often need to jump between them, it’s very useful to have an alias that takes you to the root of projects. This can be achieved by adding an entry on your ~/.bash_aliases file. Let’s say all your projects are stored on /data/www, then, adding this line should be enough: www=’cd /data/www’. After that, you can directly move to your projects parent directory, just by typing www.

However, if you are as lazy as me, you may want to go beyond that. Probably, you want to jump directly to an specific project, instead of the parent directory. You cannot  pass a variable to an alias, but you can create your custom function on the ~/.bashrc file. In my particular case, all my web projects are stored like this: “/data/www/dev.SITENAME.(com|es|org)/SITENAME”, so that’s what I did:

#Helper to quickly jump to an specific project
function cdd() {
cd /data/www/dev.$@*/$@
}

Now, you can quickly jump to any project just by typing cdd and the project’s name. However, you still need to write the project name properly, which is a bit painful if the names are very long or even not in your language. Wouldn’t it be perfect if you could type cdd and then hit [TAB] for autocompletion with the list of all the available projects? Yes, we can! 🙂
Basically, we need another function that, given any input characters, outputs all the projects that begin with the specified name (or all, if no character was specified). Then, we will use this function as complete of the cdd previously created:

#Autocomplete for tab key displaying all installed sites
function _sitedirs(){
local curw
COMPREPLY=()
curw=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($(compgen -W '`ls /var/www/ | cut -d"." -f2`' -- $curw))
return 0}

Basically, I make an ls of the projects folder, then I cut the folder with the dot delimiter, and take just the second field, which contains the names of the projects. Then, I filter by the given input and store the result in COMPREPLY.

Finally, to get this working, just add the following line, that will output the available projects once you hit [TAB] key after typing cdd:

#Assigning the complete method to my custom functions
complete -F _sitedirs -o dirnames cdd

If you want to learn more about how bash completion works, I recommend you to read this articles.

Note: Each time you edit your .bash_aliases or .bashrc, the changes will not take effect until you re-login, but you can reload them just by typing:

source ~/.bashrc

One thought on “Customizing bashrc, bash aliases, and more – I

  1. Pingback: Customizing bashrc, bash aliases, and more – II • The Developer World Is Yours

Leave a Reply

Your email address will not be published. Required fields are marked *