1

Any time I open multiple files I'd like emacs to only show me one buffer. I do not want any split windows. This would save me a keystroke every time.

I learned I could do away with the buffer list from this post with (setq-default inhibit-startup-buffer-menu t). But, now another buffer just replaces what would have been the buffer list. Any way to force only one window when opening multiple files?

4
  • If I understand correctly, you open files with emacs file1 file2 and want to avoid the windows splitting by default at startup? IMO, it's better to open Emacs then find-file as you don't have any startup latency. Nov 2 '20 at 1:49
  • 1
    That would be a workaround, yes, but I have bash functions that open up workflows and projects, for which it would be tedious to C-x C-e every file. Latency has never been a noticeable issue. Nov 2 '20 at 2:14
  • 2
    Do you want something like this perhaps: emacs --eval '(find-file-noselect "a")' --eval '(find-file-noselect "b")' c ? This opens a and b but does not select them and then opens c.
    – NickD
    Nov 2 '20 at 2:25
  • That would do the job, yes, but I was hoping for a solution that allowed me to set the behavior in my init so that I can continue to open files normally, now with the desired behavior. It's not that important to me to choose which file gets the single window. Nov 2 '20 at 17:12
1

I don't quite understand your last comment, but try the following in your init file:

(setq inhibit-splash-screen t)
(setq command-line-functions (list #'handle-files))

(defun handle-files ()
  (let* ((files (cons argi command-line-args-left))
         (lastfile (car (last files)))
         (files (butlast files)))
    (while files
      (find-file-noselect (car files))
      (setq files (cdr files)))
    (if lastfile
      (find-file lastfile))
    (setq command-line-args-left nil)
    t))

The assumption is that all the options are already processed and you just have a list of files at the end of your command line. Each function in the list command-line-functions is called with no args (do C-h i g (elisp)command-line-arguments to read the relevant section of the Elisp manual in emacs or see the online Elisp manual for details). There is only one function in this case and it consumes all of the left-over arguments: it does a find-file-noselect on all of them except the last one, on which it does a find-file.

Doing emacs -Q -l /path/to/init/file.el a b c d does what you want (I think).

EDIT: per the OP's comment, I fixed up the code so that it works with 0, 1 and 2 file arguments. By induction, it has to work for any number of arguments :-)

10
  • Thank you for putting this together. It does do the desired behavior, but what I meant in the last comment is I'd like to put a function or setting in my init and then open files as normal with emacs a b c d. Would it be simpler to add some sort of hook that executes delete-other-windows after opening files from the command line as described? Nov 11 '20 at 16:52
  • That's exactly what this is supposed to do: did you try putting it into your init file and then starting emacs?
    – NickD
    Nov 11 '20 at 19:44
  • It does, but I now have to issue it with the -Q -l options. I think adding a hook to delete-other-windows would be the cleanest solution. I'm not sure what to hook on though? If you come up with an answer like that I'll mark it as solved. Nov 20 '20 at 17:10
  • No, just put the code in your init file as is and then restart normally: no -Q -l needed. It should just work.
    – NickD
    Nov 20 '20 at 17:41
  • That's odd, maybe I was doing something wrong. Still, don't you think a hook would be much cleaner? Nov 23 '20 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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