Customizing bashrc, bash aliases, and more – II

On my the previous post, I explained a few tricks that I use in order to move faster through my project. Básically, you can achieve that by customizing bashrc file on your home directory.

Before moving on to a new topic, I’d like to explain some more tips related to my last post. We created a function called “_sitedirs” that was used just as completion of the input of our function. Well, we can take profit of this function and create many more helpful functions.

For instance, if our application logs all the debug and error messages within a project-relative path, we could easily create a function to display them. In my case, all the projects are running on Magento, so by default the logs are being stored on Project/var/log/*.log. Then It’s fairly easy to display the app logs, but I wanted to display the webserver errors as well. I’m using Nginx, and all the project’s configuration are set to save the logs at /var/log/nginx/Proiect.*.log, where Project is exactly the same name of the proyect’s folder. There are two log files for each nginx’s site: the access and errors. I’m only interested on showing the errors, so this is how my function looks:


#Helper to display project log
function logg() {
 tail -f /var/www/dev.$@*/$@/var/log/* /var/log/nginx/dev.$@.*.error.log
}

And in order to use the tab completion, we just need to add the following line:

complete -F _sitedirs -o dirnames logg  

Now, I can type logg, then type any key and/or press [TAB] to complete the name of the project, and that’s it, you can browse trough your project and check out the console to debug it.

There are many other possible usages, like clearing the cache, enabling/disabling maintenance mode, and many more, your only limit is your imagination!

That’s all from now, on my next post I’ll try to explain my aversion to the passwords and how I try to work without having to type a single one (whenever it’s possible).

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