Some buffers are created by Emacs and is set in fundamental mode, like the *Org-Babel results*. I think it is more convenient to set them in special mode, which can be closed with key q. How do set the mode for those automatic buffers or if the name contains *?

Lawlist and Drew's comments below show possible ways to do it, what I want is a special-display buffers for some buffers with *...* names. The appropriate way of course is to find the function for creating the buffer and change it in the package, but it doesn't suit everybody.

Since special-display-regexps is obsolete, can anyone help me on an display-buffer-alist with a custom function to look for *Org-Babel Results* and set it to special mode?

  • Do you want to set the mode or bind a key?
    – Dan
    Commented Jun 16, 2015 at 22:58
  • 2
    It would be better, in my opinion, to locate each of the functions responsible for creating the buffers in the first place and enable the custom mode at that point -- i.e., modify the functions directly responsible. Anything else is just an after-the-fact-hack (in my opinion). It is really easy to find with grep -- i.e., just look for the name of the buffer in the source code, and things like get-buffer-create, display-buffer, pop-to-buffer, etc.
    – lawlist
    Commented Jun 16, 2015 at 23:56
  • 1
    For example, after (pop-to-buffer (get-buffer-create "*Org-Babel Results*")) or after the next line -- i.e., (delete-region (point-min) (point-max)) -- you could add a line (with-current-buffer (get-buffer "*Org-Babel Results*") (enable-my-custom-major-mode)) In my opinion, this is much more precise/localized than customizing the display-buffer-alist with a custom function to look for *Org-Babel Results*, which is another possibility, but I don't recommend broad brush approaches like that.
    – lawlist
    Commented Jun 17, 2015 at 0:18
  • 1
    Personally, I don't find the question unclear, he wants special-mode to be activated on org-results buffers (and other buffers with similar name).
    – Malabarba
    Commented Jun 17, 2015 at 7:45
  • 1
    @godblessfq The question in your title (major mode for non-file buffers) is a duplicate of this one. Would you consider editing your question and title to be more specific to org-results buffers? (because I don't think any of the answers on the other question would work on the org-results buffer)
    – Malabarba
    Commented Jun 17, 2015 at 7:50

2 Answers 2


Thanks to lawlist. I finally got it working.

(defun db-regexp-match-p (regexps string)
  (and string
       (catch 'matched
         (let ((inhibit-changing-match-data t))
           (dolist (regexp regexps)
             (when (string-match regexp string)
               (throw 'matched t)))))))
(defvar special-buffer-regexp
  '("[*].*results.*[*]" "[*].*out.*[*]")
  "Regexp of special mode buffer names")
(defun set_special_mode (buffer alist)
  (if (db-regexp-match-p special-buffer-regexp (buffer-name buffer))
  (with-current-buffer buffer 

(add-to-list 'display-buffer-alist '(".*" . (set_special_mode)))
  • (setq display-buffer-alist '((".*" . (set_special_mode)))) -- it may [?] be possible to use add-to-list instead of setq, but you are missing two (2) pairs of parentheses and a period in either event.
    – lawlist
    Commented Jun 24, 2015 at 0:12
  • Fixed. Thank you! But it appear to work before the fix. Apparently I need to learn more elisp. :-)
    – godblessfq
    Commented Jun 25, 2015 at 1:51
  • If you look at your edit history, you'll see that you had it essentially right (just with a more restrictive regexp that isn't really needed since the db-regexp-match-p is doing the work), but then you accidentally broke it when you used add-to-list -- at that point in the edit history, you lost a period and you lost a couple of pairs of parentheses.
    – lawlist
    Commented Jun 25, 2015 at 2:21
  • The latex output buffer doesn't work even after the fix.
    – godblessfq
    Commented Jun 25, 2015 at 13:48
  • You can use either of the following examples: (add-to-list 'display-buffer-alist '(".*" . (set_special_mode))) or (setq display-buffer-alist '((".*" . (set_special_mode)))) You can test either example by evaluating (display-buffer (get-buffer-create "*filename output*")) If running a test with add-to-list, be sure to return the variable display-buffer-alist to a nil value so that you don't end up stacking your tests one on top of the other -- e.g., evaluating (setq display-buffer-alist nil) will reset the variable to a value of nil.
    – lawlist
    Commented Jun 25, 2015 at 14:55

Below is another solution which is more elegant

(setq-default major-mode
  (lambda ()
  (if (db-regexp-match-p special-buffer-regexp (buffer-name))
  • This solution may cause problems somewhere down the line because the C-source code has major-mode set up as a symbol, not necessarily a function (although it could be the symbol of a function). It so happens that fundamental-mode is a function defined within simple.el
    – lawlist
    Commented Jun 22, 2015 at 21:53
  • Even though I have enabled both solutions, I still don't have the latex output buffer (C-c C-l) in special mode. The buffer name is *filename output*.
    – godblessfq
    Commented Jun 23, 2015 at 22:38
  • I posted my comment underneath the other answer as it relates to just that one.
    – lawlist
    Commented Jun 23, 2015 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.