Emacs Tips — Close compilation buffer


Dheepak Krishnamurthy


July 19, 2015

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



BibTeX citation:
  author = {Krishnamurthy, Dheepak},
  title = {Emacs {Tips} — {Close} Compilation Buffer},
  date = {2015-07-19},
  url = {https://kdheepak.com/blog/emacs-tips-close-compilation-buffer-if-successful},
  langid = {en}
For attribution, please cite this work as:
D. Krishnamurthy, “Emacs Tips — Close compilation buffer,” Jul. 19, 2015. https://kdheepak.com/blog/emacs-tips-close-compilation-buffer-if-successful.