extended less usage
June 28th, 2011
Ever used grep and wanted to check that file with less? Now it is easy with wrapping functions, they parse grep outputted lines.
So lets say we search for info in rvm packages:
grep -rn '__rvm_use(' ~/.rvm/scripts /home/mpapis/.rvm/scripts/selector:391:__rvm_use()
So now you want to see what is inside of ‘.rvm/scripts/selector’ at line 391
less -N +j391 /home/mpapis/.rvm/scripts/selector
But wouldn’t be nice to just write less, double click the found line and middle click:
less /home/mpapis/.rvm/scripts/selector:391:__rvm_use
This will work when using following function:
less() { local params=() param parsed for param in "$@" do case $param in (*:*) parsed=( ${param//:/ } ) params+=( "-N" "+j${parsed[1]}" "${parsed[0]}" ) ;; (*) params+=( "$param" ) ;; esac done command less "${params[@]}" }
Put the above code into ~/.functions and source it in ~/.bashrc
test -s ~/.functions && . ~/.functions || true
Now restart your shell and enjoy you extended less function.