Emacs Tips - Close compilation buffer

Dheepak Krishnamurthy

Published on

When I work with markdown, \(\LaTeX\) or code that requires compiling, I like to check often if everything looks okay. Emacs has a good description of everything you can do with the CompileCommand.

However, it does not have a description for auto-closing the buffer if the compilation was successful. StackOverflow to the rescue!

; from enberg on #emacs
(setq compilation-finish-function
(lambda (buf str)
    (if (null (string-match ".*exited abnormally.*" str))
        ;;no errors, make the compilation window go away in a few seconds
            "1 sec" nil 'delete-windows-on
            (get-buffer-create "*compilation*"))
        (message "No Compilation Errors!")))))

Insert the above code into your .emacs file. You can change the time you wish the buffer to be available by changing the “1 sec” in the above code