Question: When we use `o` in dired, it opens a file in "other window". But if there is more than one other window visible, then it appears to choose randomly. Is there a way to choose which window it should open that file in?

Answer: 
I was annoyed by that behaviour of dired too. I like the way [ace-window](https://github.com/abo-abo/ace-window) lets you easily select windows with only one key stroke, so a wrote a function that would let me use that to open a file from dired in a window of my choice: 

```
(require 'ace-window)
(defun find-file-ace-window ()
  "Use ace window to select a window for opening a file from dired."
  (interactive)
  (let ((file (dired-get-file-for-visit)))
    (if (> (length (aw-window-list)) 1)
        (aw-select "" (lambda (window)
                        (aw-switch-to-window window)
                        (find-file file)))
      (find-file-other-window file))))

```

To replace dired standard `find-file-other-window` command add

```
(define-key dired-mode-map "o" 'find-file-ace-window)

```
I doubt the behaviour is random, but you can change it by hooking into `display-buffer` and binding your own command.

Note that most of the code is to sort window list by area.

```
(defvar le::dired-chosen-window nil
  "current chosen window to show buffers for `le::dired-choose-window-action'")

(defun le::window-list-for-completion ()
  "Return alist of (BUFFER-NAME . WINDOW) sorted by size,
  suitable for completion."
  ;; Schwartzian transform
  (mapcar
   (lambda (cell)
     (let ((window (car cell)))
       (cons (concat
              (when (and (boundp 'window-numbering-mode)
                         window-numbering-mode)
                (format "%s: "
                        (window-numbering-get-number-string window)))
              (buffer-name (window-buffer window)))
             window)))
   (sort (loop with boost-alist = (if (eq major-mode 'dired-mode)
                                      (list (cons (selected-window) -1000000))
                                    nil)
               for window in (window-list nil 0 (window-at 0 0))
               for index from 0
               collect (cons window (+
                                     (* (window-total-width window)
                                        (window-total-height window)
                                        100)
                                     index
                                     (let ((boost (assq window boost-alist)))
                                       (if boost
                                           (cdr boost)
                                         0)))))
         (lambda (a b)
           (> (cdr a) (cdr b))))))

(defun le::dired-choose-window (window)
  "Choose window to display buffer in.
Reset selection with universal prefix (C-u)."
  (interactive (let ((completions (le::window-list-for-completion)))
                 (list (unless current-prefix-arg
                         (cdr (assoc (completing-read "window holding buffer: " completions) completions))))))
  (setq le::dired-chosen-window window))

(defun le::dired-choose-window-action (buffer alist)
  "action to display buffer in `le::dired-chosen-window'"
  (window--display-buffer buffer le::dired-chosen-window 'reuse))

(defun le::dired-find-file-specified-window ()
  "Dired command to display buffer in chosen window"
  (interactive)
  (let ((display-buffer-overriding-action '((le::dired-choose-window-action))))
    (dired-find-file-other-window)))

(define-key dired-mode-map [remap dired-find-file-other-window] 'le::dired-find-file-specified-window)

```

1. Evaluate buffer.
2. choose the window with `le::dired-choose-window-action`
3. press `o` from dired to open file in chosen window.