Some bash goodness

Here’s some bash goodness (well, not really) to make ‘less’ a little bit more useful.
 
less() {
       local -a args
 
       for arg
       do
               case “$arg” in
               *:[[:digit:]]* )
                       line=${arg/#+(?):/}
                       args[${#args[*]}]=”+$line”
                       arg=${arg/%:+(?)/}
                       ;;
               esac
 
               args[${#args[*]}]=$arg
       done
 
       $( which less ) “${args[@]}”
}
 
Basically, this shell function loads when you tell it to (in your .profile, likely), and it replaces the ‘less’ command. When you type ‘less …’ on the command line, the function runs.
 
It scans through the args, looking for one like filename.c:24, and if it finds that kind of arg, it replaces it with +24 filename.c — translating the syntax used by a *lot* of compilers into the syntax used by less for opening a file and jumping directly to the line number.
 

Leave a Reply