What I must to write in my .emacs file so the *scratch* buffer is closed when I open Emacs?
Not exactly the answer to your question, but you might like to know that you can choose to have a different buffer open on startup, or change the contents of the scratch buffer. For example:
(setq initial-scratch-buffer nil)
(setq initial-buffer-choice user-init-file)
In the first example, the scratch buffer will be empty. In the second example, the scratch buffer will still exist, but user-init-file will be focused.
I suspect from your question that you probably start emacs fairly often, perhaps even once for each file you want to edit. (If I'm wrong in this assumption, then the following comments don't apply to you.)
Emacs is designed to be started and then left running for weeks or months while you visit various files as you need to edit them. Emacs handles multiple files very well, so it's hardly even necessary to kill the associated buffers until you get 50 or 100 of them hanging around. I start emacs just after my window system starts, and it runs until my system shuts down or crashes. The initial scratch buffer is a non-problem in this mode, because I see it so infrequently.
You can customize:
initial-buffer-choice
I set it to my homedir: "~/" to start in Dired mode.
I use this to kill the scratch buffer and open a new buffer in text mode called Untitled. Found it on a newsgroup and modified it slightly.
(defun my-close-scratch () (kill-buffer "scratch") (if (not (delq nil (mapcar 'buffer-file-name (buffer-list)))) (new-untitled-buffer) ))
(defun my-emacs-startup-hook () (my-close-scratch)) (add-hook 'emacs-startup-hook 'my-emacs-startup-hook)
(defun new-untitled-buffer () "Opens a new empty buffer." (interactive) (let ((buf (generate-new-buffer "Untitled"))) (switch-to-buffer buf) (normal-mode) (setq buffer-offer-save t)) (add-hook 'kill-buffer-query-functions 'ask-to-save-modified nil t) )
To close Untitled when opening files from filemanager when emacs is not open I use this:
(defun my-close-untitled () (if (get-buffer "Untitled") (kill-buffers-by-name "Untitled")))
(add-hook 'find-file-hook 'my-close-untitled)