How can I quickly view multiple files in Emacs?

Is there a way to quickly view many files in Emacs (24.3)? More specific:

Assume that the Emacs frame is split into two windows. Suppose that the focus is in the left window, in which there is an open "dired" buffer with a large number of text files (or code). I would like to move up and down the list of files (for example, using the cursor keys), while the current file is displayed in the right window. Even better, the file is viewed and closed only after I move to the bottled buffer to the next file. This would be very useful, especially in the "lower" mode.

Can this be done in "dired"? I also cannot find this function in dired-x or in the sunrise-commander command. Is it possible?

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

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

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

Many thanks for your help!

+7
emacs dired
source share
6 answers

Thanks so much for all these answers. To summarize, I created the following solution (expanding the answer "abo-abo"):

;; little modification to dired-mode that let you browse through lots of files (add-hook 'dired-mode-hook (lambda() (define-key dired-mode-map (kbd "Co") '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 changed:

  • Co to view the current item in another window (possibly create one).
  • n to view the next item in another window.
  • p to view the previous item in another window.

This can be used in a buffer with a buffer. Please note that only buffers with real-time buffers and view buffers are killed when moving up and down. If the file indicates that another buffer is already visiting (not in view mode), this buffer is also displayed, but is not killed when moving to the next one. Another subtlety is the case when the passively displayed buffer is the buffer used to go through the list (this can be easily done when moving to the folder with RET ). To deal with this case, we first check to see if we are trying to kill the initial overwritten buffer.

+2
source share

Here is how I do it with view-mode :

 (add-hook 'view-mode-hook (lambda() (define-key view-mode-map (kbd "n") 'dired-view-next) (define-key view-mode-map (kbd "p") 'dired-view-prev))) (defun dired-view-next () "Move to next dired line and view ." (interactive) (quit-window) (dired-next-line 1) (dired-view-file)) (defun dired-view-prev () "Move to next dired line and view ." (interactive) (quit-window) (dired-next-line -1) (dired-view-file)) 

UPD:

This has two panels:

 (defun dired-view-next-pane () (interactive) (other-window 1) (if view-mode (kill-buffer)) (other-window -1) (dired-next-line 1) (view-file-other-window (dired-get-file-for-visit)) (other-window -1)) 
+2
source share
  • Download Icicles .

  • Define this command:

  (defun my-find-file ()
       "Like` icicle-find-file ', but alt action views file temporarily.
     Alternate action keys such as `CS-down 'visit the candidate file in
     `view-mode 'and kill the buffer of the last such viewed candidate."
       (interactive)
       (let ((icicle-candidate-alt-action-fn
              (lambda (file)
                (when (and my-last-viewed
                           (get-file-buffer my-last-viewed))
                  (kill-buffer (get-file-buffer my-last-viewed)))
                (setq my-last-viewed (abbreviate-file-name file))
                (view-file file)
                (select-frame-set-input-focus
                   (window-frame (active-minibuffer-window))))))
         (icicle-find-file-of-content)))

     (defvar my-last-viewed nil
       "Last file viewed by alternate action of` my-find-file '. ")

Then you can:

  • Use Mx my-find-file (or attach it to a key - for example, Cx Cf ).
  • If necessary, enter a part of the file name to restrict matching names.
  • If necessary, use down or up to cycle through the file names.
  • Use CS-down to view the next file later.
  • Repeat # 4 to see other files in order.
  • Repeat # 2 or # 3 to see other sets of files.
  • Finish with RET to select the file to visit, or Cg to cancel.

Each file buffer that you visited using CS-down was killed when you looked at the next. You can also mix in C-down or C-RET before also visiting files whose buffers you do not want to kill automatically. (Change the view-file to find-file , if you do not want to visit the view-mode , which is read-only.)

[By default, the alternate action for icicle-find-file is icicle-alt-act-fn-for-type , which asks for the file-related actions to use on the particular candidate selected for the Action. The my-find-file simply replaces another alternative action function (for all selected candidates).]

See also this thread from help-gnu-emacs@gnu.org. I think this is almost the same question as yours. My answers were almost the same as my answer here, but there are also answers from others that can help you.

+2
source share

Try

 Mx speedbar 

You may like it

+1
source share

A simple and general (although not optimal) solution may be the Cx () mechanism.

First open the two panels in Emacs, using - say - the top one that you want to smooth out.

  • Press o to open the first file in panel 2 nd .

Then you can start the repetition mechanism:

  • do Cx ( to start recording a macro
  • do Cx k and return to close the buffer
  • do o go back to obsolete again
  • do down to go to the next file
  • do o to open the next file in the bottom pane
  • do Cx) to complete the macro

From this point (being in the bottom pane, numbered in the top pane), doing just

  • Cx e (and then only e if there is no other operation between them)

will be automatically

  • close the bottom panel file, go to the top panel, go to the next file, open it in the bottom panel

There may be a more specific way to do this, but knowing the macro mechanism is very useful in Emacs anyway.

+1
source share

Another solution in view mode over ag-mode lists. I could not find a question for ag-mode, maybe this helps someone generalize ffap-preview for any mode.

 (defun directory-ag-results () (save-excursion (goto-char (point-min)) (search-forward "\"") (setq a (point)) (search-forward "\"") (setq b (- (point) 1)) (buffer-substring-no-properties ab))) (defun search-item-path () (let ((dir (directory-ag-results)) (file-parts (split-string (substring-no-properties (thing-at-point 'filename)) ":"))) (concat dir (nth 0 file-parts)))) (defun search-item-line () (let ((file-parts (split-string (substring-no-properties (thing-at-point 'filename)) ":"))) (- (string-to-number (nth 1 file-parts)) 1))) (defun view-current () "Quickly view the current file in another window." (if (not (window-parent)) (split-window)) ; create a new window if necessary (let ((file (search-item-path)) (line (search-item-line)) (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 (progn (view-file file) ; ... view it (goto-char (point-min)) (next-line line))) (other-window -1)))) (defun next-view-current () (interactive) (next-line) (view-current)) (defun previous-view-current () (interactive) (previous-line) (view-current)) (define-key ag-mode-map (kbd "Mp") 'previous-view-current) (define-key ag-mode-map (kbd "Mn") 'next-view-current) 

This is the only thing I think Sublime is better than Emacs. Blasphemy, I know! I like the "q exit" from view mode rather than by timer, and like scrolling around the file being viewed. This fragment moves to the line number found in the search results, optimizing the browsing speed.

Pay attention to the code: I tried polyfilling vc-root-dir from Emacs 25, but this does not make much sense for ag-mode, since the ag-mode buffer is outside the repo you are looking for. pulling the root directory from the top of the ag search search buffer.

Early stages. Improvements are welcome.

Demo

Edit: it works for ag-mode, not the default. Demo version.

Credits: abo-abo, user2979331

0
source share

All Articles