Håkon Hægland
Håkon Hægland

Reputation: 40718

Select other window in Emacs at startup

Suppose I have two text files A.txt and B.txt. I choose to open both from Emacs using

emacs -no-splash -mm A.txt B.txt

Now the frame is split in two parts vertically, and file A is shown in the left window and file B in the right window. However file B is automatically selected by Emacs. I can move the point to the other window by entering C-x o or ESC-: (other-window 1). But I would like to do this automatically, for instance at the command line or in the .emacs file.

I tried

emacs -no-splash -mm --eval `(other-window 1)` A.txt B.txt

but it did not work..

Upvotes: 1

Views: 362

Answers (5)

Drew
Drew

Reputation: 30701

Is there a reason you don't want to just reverse the order of the file names on the command line?

Upvotes: 0

Håkon Hægland
Håkon Hægland

Reputation: 40718

The following seems to work: Enter in .emacs :

(add-hook 'emacs-startup-hook '(lambda () (other-window 1)))

Upvotes: 3

Andreas Röhler
Andreas Röhler

Reputation: 4804

This would keep a single window with "B.txt"

emacs --find-file A.txt B.txt -Q -eval "(delete-other-windows)"

Upvotes: 0

Thomas
Thomas

Reputation: 17412

Independent of the order of the command line parameters, this approach finds the left-most, upper-most window and selects it. Put the following code in your .emacs file:

(add-hook 'emacs-startup-hook '(lambda () (select-upper-left-window)))

(defun select-upper-left-window ()
  (let (window)
    (while (setq window (window-in-direction 'above nil t))
      (select-window window))
    (while (setq window (window-in-direction 'left nil t))
      (select-window window))))

Upvotes: 0

Thomas
Thomas

Reputation: 17412

Put the following in your ~/.emacs file:

(add-hook 'emacs-startup-hook '(lambda () (select-first-buffer-in-list command-line-args)))

(defun select-first-buffer-in-list (list)
  (let (buffer)
    (while list
      (if (setq buffer (get-file-buffer (car list)))
      (progn (select-window (get-buffer-window buffer))
         (setq list nil))
    (setq list (cdr list))))))

It will check which of the command line parameters correspond to a buffer. It selects a window displaying the buffer of the first such parameter.

Upvotes: 1

Related Questions