4 ms·
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
by ww520 1y ago
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)
- skydhash 1y agoAn alternative workflow I use is the dired-view-file bound to 'v' to have a read-only buffer for the file which you can scroll through with <space> and other such keys. Then you can use 'q' to close the buffer and 'e' to edit it.
- ww520 1y agoYes, that works but the view window takes up the whole frame. I want the dired window remains and the cursor/focus to remain in the dired window so that I can go down the file list and see the file content on the other window. Ctrl-n and ctrl-o are all I need to do.
- skydhash 1y agoI got you. I like to be focused on a single thing so whatever is in front of me is relevant to the current task: Either selecting files or viewing them. So I've never been a fan of the file explorer windows/dock in most editors and IDEs. In Vim, I use vinegar, which bind the minus key to display the file's directory in netrw and in netrw, go up.
- ww520 1y agoYes, I like to focus on one thing, too. That's why I usually only have one window and one buffer open to work on. The list-detail split view mode is when I want to quickly browse the files in a directory, like when I'm looking into a unfamiliar project.
- phforms 1y agoI guess you already know the command `dired-find-file-other-window` (bound to `o` by default), which reuses the right split window, if already there and opens a new window if there is none. Horizontal splits do not seem to be reused though (they often don’t have enough vertical space for dired anyway, which is why I guess they designed it this way).
- ww520 1y agoYes. dired-find-file-other-window is the one I found annoying. It moves the cursor/focus to the other window, and it keeps opening new split windows. If I ran the cmd 3 times, it would open 3 new split windows. Really annoying. I want the cursor/focus to remain on the dired window so that I can go down the file list and see the file content on the other window. Ctrl-n and ctrl-o are all I need to do.
- phforms 1y agoOkay, I can see the issue now, maybe I haven’t really used dired intensely enough for it to bother me. I just tried your snippet and having the cursor remain on the dired buffer for a quick preview is a feature I didn’t know I needed. Thanks for sharing!