Search code examples
emacsdired

How can one quickly browse through lots of files in Emacs?


is there a way to quickly browse through lots of files in Emacs (24.3)? More specifically:

Let's assume an Emacs frame is split into two windows. Suppose focus is in the left window that has an open 'dired' buffer with lots of text files (or code). I would like to go up and down the list of files (e.g. with cursor keys), while at the same time the current file is shown in the right window. Even better the file is only viewed and closed once I move in the dired buffer to the next file. This would be very useful especially together with some 'omit' mode.

Can this be done in 'dired'? I also coudn't find this functionality in dired-x or in sunrise-commander. Is it possible?

The best candidates I tried already (and why they not solve the problem):

'v' which shows the current file, but also moves the attention

'C-o' which shows the current file, but after moving up or down, I have to press C-o again, also it generates lots of buffers

Thanks a lot for your help!


Solution

  • Thanks a lot for all those answers. Summarizing I created the following solution (extending the answer of "abo-abo"):

    ;; little modification to dired-mode that let's you browse through lots of files
    (add-hook 'dired-mode-hook
      (lambda()
        (define-key dired-mode-map (kbd "C-o") 'dired-view-current)     ; was dired-display-file
        (define-key dired-mode-map (kbd "n")   'dired-view-next)           ; was dired-next-line
        (define-key dired-mode-map (kbd "p")   'dired-view-previous))) ; was dired-previous-line
    
    (defun dired-view-next ()
      "Move down one line and view the current file in another window."
      (interactive)
      (dired-next-line)
      (dired-view-current))
    
    (defun dired-view-previous ()
      "Move up one line and view the current file in another window."
      (interactive)
      (dired-previous-line)
      (dired-view-current))
    
    (defun dired-view-current ()
      "View the current file in another window (possibly newly created)."
      (interactive)
      (if (not (window-parent))
          (split-window))                                   ; create a new window if necessary
      (let ((file (dired-get-file-for-visit))
            (dbuffer (current-buffer)))
        (other-window 1)                                          ; switch to the other window
        (unless (equal dbuffer (current-buffer))                 ; don't kill the dired buffer
          (if (or view-mode (equal major-mode 'dired-mode))   ; only if in view- or dired-mode
              (kill-buffer)))                                                    ; ... kill it
        (let ((filebuffer (get-file-buffer file)))
          (if filebuffer                              ; does a buffer already look at the file
              (switch-to-buffer filebuffer)                                    ; simply switch 
            (view-file file))                                                    ; ... view it
          (other-window -1))))                   ; give the attention back to the dired buffer
    

    Three keys are changed:

    1. C-o to view the current item in another window (possibly create one).
    2. n to view the next item in another window.
    3. p to view the previous item in another window.

    This can be used in a dired buffer. Note that only dired-mode buffers and view-mode buffers get killed while moving up and down. If a file is shown that another buffer is already visiting (not in view-mode), that buffer is shown as well, but not killed when moving to the next. Another subtlety is the case when the passively shown buffer is the dired buffer used for going through the list (this can easily happen, when going inside a folder with RET). To handle this case, we first check whether we are trying to kill the initial dired buffer.