Suppose I want Emacs to be configured differently depending on whether or not it was launched with a filename as an argument. Is there a way I can deduce this as part of my init file?

One way is to check if there are any buffers open:

(require 'cl-lib)

(defun non-user-buffer (buffer)
  (let ((name (buffer-name buffer)))
    (when (or (string-prefix-p " " name)
              (and (string-prefix-p "*" name) (string-suffix-p "*" name)))

(defun num-user-buffers ()
  (length (cl-remove-if 'non-user-buffer (buffer-list))))

;; Provided recently launched...
(defun launched-with-buffer ()
  (when (> (num-user-buffers) 0) t))

You have access from Emacs Lisp to information about the command line, such as its arguments (variable command-line-args).

See the Elisp manual, node Command Line Arguments.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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