Using less with executables?

2015-12-27

I'd been using a custom alias to view object dumps for a long time. TIL that I could have modified less to do this all along! You can put code into ~/.lessfilter that tries to produce a character stream from whatever it's given. This file must be executable. This filter runs before the longer list of filters in the lesspipe program run. Executables are not in the default set (they shouldn't be of course), but it's useful for me:
#!/bin/sh

case "$1" in
    # Possibly handle other cases here in the future
    *)
        file $1 | grep -Eq 'executable|shared object'
        if [ $? -eq 0 ]; then
            objdump -Cfd --section=.text -M intel-mnemonic $1
            exit 0
        fi
        # We don't handle this format.
        exit 1
esac

# No further processing by lesspipe necessary
exit 0
Now I can type less a.out and see a nice dump! P.S: On Debian/Ubuntu, it's standard to have this in your ~/.bashrc:
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
That's needed for the above to work.