See Tobias' answer for the simplest and, in the face of user customisations, more robust solution.
Just for fun, I'd like to share an alternative approach based on an answer of mine to a similar question.
The idea is that, during startup, the only buffers that are usually open, other than standard splash/scratch/etc. buffers, are ones due to unrecognised command-line arguments. For example, emacs -q foo
results in a file-visiting buffer named foo
being displayed.
This allows us to set the user option initial-buffer-choice
to a function which focusses the Org Agenda for the case when there are no unrecognised CLI arguments, and then revert the variable to its original value when unrecognised CLI arguments are detected. See (elisp) Command-Line Arguments
for more information on this API.
Without further ado, the required incantations:
(defun my-org-agenda-focus ()
"Display nothing but the Org Agenda in the selected frame.
Return `current-buffer' for `initial-buffer-choice'."
(org-agenda-list)
(delete-other-windows)
(current-buffer))
(defun my-detect-cli-args ()
"Inhibit `my-org-agenda-focus' on unrecognised CLI args.
Return nil, i.e. leave arguments unprocessed."
(ignore (setq initial-buffer-choice nil)))
(add-hook 'command-line-functions #'my-detect-cli-args)
(setq initial-buffer-choice #'my-org-agenda-focus)
(setq inhibit-startup-screen t)
Note that, starting with Org 9, you can also write
(defun my-org-agenda-focus ()
"Display nothing but the Org Agenda in the selected frame.
Return `current-buffer' for `initial-buffer-choice'."
(let ((org-agenda-window-setup 'only-window))
(org-agenda-list))
(current-buffer))
The approach of OP and Tobias, when hooked into emacs-startup-hook
or window-setup-hook
, has the advantage of being simpler and running after most, if not all, buffers, windows and frames have been created.
The CLI arg approach is more beneficial for fine-grained inspection of each argument. For example, you could take different actions depending on whether Emacs was invoked with a special keyword, a directory, a file or a non-existent file argument.
Ultimately you can combine both approaches for the superlative Emacs customisation experience.
after-init-hook
? Note, that this hook is run before the files specified on the command line are opened. Maybe, you should useemacs-startup-hook
. – Tobias Jan 12 '18 at 15:51