0

I want to kill some buffers created by default, such as the GNU Emacs, but putting:

(kill-buffer "*GNU Emacs*")

On the .emacs doesn't work. I guess this code is called before the initial buffers are created. There should be some kind of hook, but I want it to be run only at the start up, to clean all the initial buffers I don't need.

Edit:

Tried after-init-hook and emacs-startup-hook without success.

Edit 2:

Put some code on .emacs to see what's happening:

(defun buffer-hook-function ()
  (append-to-file (format "%s" (buffer-list)) nil  "log.txt")
  (append-to-file "\n" nil  "log.txt")
  )

(append-to-file "Executou .emacs\n" nil  "log.txt")

(add-hook 'emacs-startup-hook (lambda () (append-to-file "emacs-startup-hook\n" nil  "log.txt")))
(add-hook 'after-init-hook (lambda () (append-to-file "after-init-hook\n" nil  "log.txt")))
(add-hook 'before-init-hook (lambda () (append-to-file "before-init-hook\n" nil  "log.txt")))
(add-hook 'window-setup-hook (lambda () (append-to-file "window-setup-hook\n" nil  "log.txt")))
(add-hook 'emacs-startup-hook (lambda () (append-to-file "window-setup-hook\n" nil  "log.txt")))
(add-hook 'buffer-list-update-hook 'buffer-hook-function)

After starting emacs, the output on log.txt is:

Ran .emacs
(*scratch*  *Minibuf-0* *Messages*  *code-conversion-work*  *Echo Area 0*  *Echo Area 1*)
after-init-hook
window-setup-hook
emacs-startup-hook
window-setup-hook
(*scratch*  *Minibuf-0* *Messages*  *code-conversion-work*  *Echo Area 0*  *Echo Area 1*  *temp*)
(*scratch*  *Minibuf-0* *Messages*  *code-conversion-work*  *Echo Area 0*  *Echo Area 1*)
(*scratch*  *Minibuf-0* *Messages*  *code-conversion-work*  *Echo Area 0*  *Echo Area 1* *GNU Emacs*)
(*scratch*  *Minibuf-0* *Messages*  *code-conversion-work*  *Echo Area 0*  *Echo Area 1* *GNU Emacs*  *temp*)
(*scratch*  *Minibuf-0* *Messages*  *code-conversion-work*  *Echo Area 0*  *Echo Area 1* *GNU Emacs*)
(*scratch*  *Minibuf-0* *Messages*  *code-conversion-work*  *Echo Area 0*  *Echo Area 1* *GNU Emacs*  *temp*)
(*scratch*  *Minibuf-0* *Messages*  *code-conversion-work*  *Echo Area 0*  *Echo Area 1* *GNU Emacs*)
(*scratch*  *Minibuf-0* *Messages*  *code-conversion-work*  *Echo Area 0*  *Echo Area 1* *GNU Emacs*)
(*GNU Emacs* *scratch*  *Minibuf-0* *Messages*  *code-conversion-work*  *Echo Area 0*  *Echo Area 1*)

I'm not finding a hook for being executed after all those initial buffers are created... Maybe I'll need to do some shenanigan to capture this event.

Drew
  • 75,699
  • 9
  • 109
  • 225

2 Answers2

3

To prevent Emacs creating the *GNU Emacs* buffer, you can set inhibit-startup-screen to t, or you can also change the user option initial-buffer-choice to tell Emacs to display a buffer that you want.

To kill *scratch*, put (kill-buffer "*scratch*") anywhere in your init file. For *Messages*, you can't avoid it since it's a special buffer which is used by Emacs itself.

xuchunyang
  • 14,302
  • 1
  • 18
  • 39
  • Thank you. Your answer is correct and also I didn't know that *Messages* couldn't be avoided. I was trying also to create a empty new buffer (http://ergoemacs.org/emacs/emacs_new_empty_buffer.html) and set it as the default initial buffer, but I couldn't capture the moment when all initial buffers were displayed so I could invoke the function then switch to the newly created buffer. With some dirty shenanigan I was able to catch this event (line 29 in https://gist.github.com/danilomo/07779d8cf28e067f47ee98f1c3432583), but I'm not that happy the way it works ¯\_(ツ)_/¯ – Danilo M. Oliveira Mar 01 '18 at 10:18
0

Easy: find and kill buffer *scratch*:

(when (get-buffer "*scratch*")
   (kill-buffer "*scratch*"))

Normal: find and kill buffer *scratch* after starting EMACS session (also with emacsclient):

(defun kill-scratch-after-init ()
  "Kill *scratch* buffer."
  (when (get-buffer "*scratch*")
    (kill-buffer "*scratch*"))

(add-hook 'after-init-hook #'kill-scratch-after-init)

or with lambda:

(add-hook 'after-init-hook (lanmda ()(
  (when (get-buffer "*scratch*")
    (kill-buffer "*scratch*")))))

Difficult: find and kill buffers from defined list after starting EMACS session:

(defun kill-initial-buffers-after-init ()
  "Kill listed EMACS buffers after initialization."
  (setq buff-list '(
                    "*scratch*"
                    "another"
                    "buffer"
                    "names"))
  (dolist (buf buff-list)
    (when (get-buffer buf)
      (kill-buffer buf)))))

(add-hook 'after-init-hook #'kill-initial-buffers-after-init)

NOTE: Buffer *Message* will be recreated automatically after first call message function.