In dired-mode, I want to have a split view of the directory listing on the left window and to quickly browse the content of each file on the right window. The default file visit command keeps opening a new window for each file which needs closing. It's really annoying. I put the following in init.el file and bind it to a key. It loads the current file under the cursor in the next existing window on right, on left, above, below, or opening a new window as needed.
;; In dired mode, visit the file at the cursor in the right/below/left/above window.
(defun my-dired-display-direction ()
(interactive)
(let* ((file-or-dir (dired-get-file-for-visit)) ;; get the file at cursor
(buffer (find-file-noselect file-or-dir))) ;; load the file into a buffer
(let ((window ;; figure out the window to use
(cond ((get-buffer-window buffer (selected-frame)))
((window-in-direction 'right)) ;; try window in each direction
((window-in-direction 'below)) ;; and default to right
((window-in-direction 'left)) ;; if no window found.
((window-in-direction 'above))
(t (split-window (selected-window) nil 'right)))))
(window--display-buffer buffer window 'window nil)
window)) )
;; Bind ctrl-o to display at direction.
(define-key dired-mode-map (kbd "C-o") 'my-dired-display-direction)