override histfile from env - fixes #831 and copies #833

This is a PR for approved feature #831 which is addressed by copying @charsyam's original PR #833 in a way that's able to be merged.