I like BAM files. So much so that I look inside one pretty much every day.

I am also lazy and a creature of habit. Hence I got tired of this:

$ less foo.bam

not working, and decided that it should.

Put this in your ~/.bashrc and less foo.bam will work for you too:

# Make less work with BAM files
original_less=`which less`
less()
{
    regex="[[:graph:]]+\.bam($|[[:space:]])"
    if [[ $@ =~ $regex ]]
    then
        samtools view $@ | $original_less
    else
        eval $original_less $@
    fi
}

If your file ends with .bam, then it is equivalent to piping samtools view into less. Otherwise, it just runs less as normal.

Saves typing at least 15 characters when you want to look in a BAM file.

Note that samtools view options etc work, such as

less -h foo.bam

or

less foo.bam contig.42:10000-20000

Disclaimer: I am not a BASH programmer. It works for me. Use at your own peril.