Open a bash command in your text editor

Thanks to this vim lighting talk I’ve just discover pressing ctrl+x, ctrl+e on bash opens your favorite text editor with the command you are typing. After saving and closing your editor the command is executed on bash.

Good when a command starts to be too long.

How could I live without knowing that?

Tweaking command-not-found to get quick access to tickets

Whenever someone send me a ticket number I have to do several steps:

  1. Alt+Tab to find Firefox
  2. Ctrl+T to get a new tab
  3. Type the Firefox keyword for the ticket system (I have “t” for trac)
  4. Search the ticket number

I don’t have many tickets because my code never fails (:D), but anyway it is quite boring to do all that.

So why not just press ALT+F2 and introduce the ticket number.
Turn out to be quite easy, I just modified the command-not-found script:

vi `which command-not-found`

and replace the line:

cnf = CommandNotFound(options.data_dir)


from subprocess import call
import numbers
if args[0].isdigit():
    call(["firefox", "-new-tab", "http://company.trac.url/ticket/" + args[0]])
    cnf = CommandNotFound(options.data_dir)

This just opens a new tab in Firefox with the ticket I type as a command.
This works on Firefox and Trac, but should be almost the same on any other browser or ticket system.

Now my productivity is boost ;-)

Much better apache log with some colors

When I have to debug some web application I often stared at the Apache log and say WTF!!
This is what the log might looks like:


The problem comes because every time I go into the web I get hundreds of unrelated log lines, a huge mess!

So one day I thought, could I improve that? My first idea was to add some color using grep like this:

tail -f /var/log/apache2/error.log | grep -E "ERROR|" --color


That is nicer because now I can see clearly errors in red.
Could that be better? Maybe yellow warnings?
The answer is yes, grep output can be concatenated to another grep and different colors can be used in every grep. Like this little script:

shopt -s expand_aliases

alias grey-grep="GREP_COLOR='1;30' grep -E --color=always --line-buffered"
alias red-grep="GREP_COLOR='1;31' grep -E --color=always --line-buffered"
alias green-grep="GREP_COLOR='1;32' grep -E --color=always --line-buffered"
alias yellow-grep="GREP_COLOR='1;33' grep -E --color=always --line-buffered"
alias cyan-grep="GREP_COLOR='1;36' grep -E --color=always --line-buffered"

tail -1000f /var/log/apache2/error.log | grey-grep ".*PerformanceLogger.*|$" | cyan-grep "INFO|$" | yellow-grep "WARN|$" | red-grep "[ERROR].*|[FATAL].*|$" | green-grep "***|$"

So the output looks like:


Which is really cool, errors and warnings are clearly highlighted and all the performance parts I don’t care about are in a dark gray that allows me to focus on what is going on.

Of course, real programmers don’t need colours, this is just for fun ;-)


< Comment! >
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||