6

Whenever I issue $ emacs, I am greeted by the startup screen. I like this.

However, when I issue $ emacs foo.txt, emacs opens foo.txt and the startup screen in a split screen. I do not like this.

Is it possible to disable the startup screen only when emacs is used to open a file?

I am not asking whether or not I can disable the startup screen (this is addressed here)

Brian Fitzpatrick
  • 2,325
  • 1
  • 20
  • 42
  • The problem with trying to fiddle with startup.el is that the code is baked into the Emacs executable. It may be possible to catch a function in the startup process and redefine it, but it may also not be possible. This is why I build my own Emacs and have my own startup.el so that I can control exactly how and when things happen when starting Emacs. It would be easier to just close the Welcome window after calling emacs foo.txt with the after-init-hook or the emacs-startup-hook, or putting the delete-other-windows at the tail end of the .emacs file. – lawlist May 18 '17 at 03:35
  • I assumed this would be possible, since this is exactly how $ emacs -nw foo.txt behaves. – Brian Fitzpatrick May 18 '17 at 04:55

1 Answers1

4

One possible approach is to exploit the hooks command-line-functions, which are called with variable argi dynamically bound to any unrecognised command-line options. The following are some of the many possible options; you can of course implement any logic you like.

The calls to ignore (always returning nil) indicate that our custom hooks have not "processed" the corresponding command-line argument. That is, we only want to effect a state change in the presence of unrecognised command-line arguments, not handle them manually.

Option 1

Assume all unrecognised options are files worthy of inhibiting the startup screen for. This option probably corresponds the most to what the default startup procedure is like.

(defun my-inhibit-startup-screen-always ()
  "Startup screen inhibitor for `command-line-functions`.
Inhibits startup screen on the first unrecognised option."
  (ignore (setq inhibit-startup-screen t)))

(add-hook 'command-line-functions #'my-inhibit-startup-screen-always)

Option 2

Only inhibit startup screen when existing files are passed as arguments.

(defun my-inhibit-startup-screen-file ()
  "Startup screen inhibitor for `command-line-functions`.
Inhibits startup screen on the first unrecognised option which
names an existing file."
  (ignore
   (setq inhibit-startup-screen
         (file-exists-p
          (expand-file-name argi command-line-default-directory)))))

(add-hook 'command-line-functions #'my-inhibit-startup-screen-file)

Option 3

Only inhibit startup screen when an unrecognised option has a file extension.

(defun my-inhibit-startup-screen-ext ()
  "Startup screen inhibitor for `command-line-functions`.
Inhibits startup screen on the first unrecognised option which
has a file name extension."
  (ignore (setq inhibit-startup-screen (file-name-extension argi))))

(add-hook 'command-line-functions #'my-inhibit-startup-screen-ext)
Basil
  • 12,383
  • 43
  • 69
  • 1
    Very cool! Option 2 is now in my .emacs. – Brian Fitzpatrick May 18 '17 at 17:55
  • 1
    @BrianFitzpatrick I just read the documentation for the customisable variable initial-buffer-choice and it relates to this startup procedure; I recommend you have a look. – Basil May 21 '17 at 09:02
  • None of the above options in the "accepted" solution work for me using GNU Emacs 27.0.50 (2019 Jan) on Mac OS. A solution that works is the one offered here, where this command is to be added to the init.el file in the ~/.emacs.d directory: (setq inhibit-startup-screen t) – Guillaume Belanger Jan 14 '19 at 17:25