]> git.earman.xyz Git - emacsinit.git/commitdiff
Dired better opening
authorrearman <rearman@r717.net>
Fri, 1 May 2026 21:10:45 +0000 (17:10 -0400)
committerrearman <rearman@r717.net>
Fri, 1 May 2026 21:10:45 +0000 (17:10 -0400)
add a defun and binding to open a file in another (re-used) window, instead of
making a new window

init.el

diff --git a/init.el b/init.el
index a7cca84a2aefa91a77624cde989145e78a08c8bc..9c8d330b1d414932353f2187f8e129919ad94366 100644 (file)
--- a/init.el
+++ b/init.el
@@ -377,6 +377,21 @@ Start from bottom if given a negative line number."
     (copy-file filepath (re/filename-inject-mod-time filepath ) nil t t t)))
 \f
 ;; Dired
+(defun re/dired-display-direction ()
+  "In dired mode, visit the file at the cursor in the right/below/left/above window."
+  (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)))
+
 (defun re/dired-new-frame ()
   "Open dired in a new frame."
   (interactive)
@@ -438,6 +453,9 @@ Start from bottom if given a negative line number."
 (add-hook 'dired-mode-hook
          (lambda ()
            (progn
+             (keymap-set dired-mode-map
+                         "o"
+                         #'re/dired-display-direction)
              ;; Rename r,R+M(mv), chmod M>c, compress-to c>z
              (keymap-set dired-mode-map
                          "z"