Emacs tips — Close compilation buffer

Author

Dheepak Krishnamurthy

Published

July 19, 2015

Keywords

tips and tricks, compilation buffer, auto close

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!

#!lisp
; 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
        (progn
            (run-at-time
            "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

Reuse

Citation

BibTeX citation:
@online{krishnamurthy2015,
  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.