Write history after every command