9

有没有办法快速浏览 Emacs (24.3) 中的大量文件?进一步来说:

让我们假设一个 Emacs 框架被分成两个窗口。假设焦点位于左侧窗口中,该窗口具有打开的“dired”缓冲区,其中包含大量文本文件(或代码)。我想在文件列表中上下移动(例如使用光标键),同时当前文件显示在右侧窗口中。更好的是,只有在我将 dired 缓冲区移动到下一个文件时,才会查看和关闭该文件。这将非常有用,尤其是与一些“省略”模式一起使用。

这可以在'dired'中完成吗?我在 dired-x 或 sun-commander 中也找不到这个功能。是否可以?

我已经尝试过的最佳候选人(以及为什么他们没有解决问题):

'v' 显示当前文件,同时也移动注意力

'Co' 显示当前文件,但是在向上或向下移动之后,我必须再次按 Co,它也会产生很多缓冲区

非常感谢你的帮助!

4

6 回答 6

4

这是我如何做到这一点的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))

升级版:

这个有两个窗格:

(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))
于 2013-11-11T14:34:30.137 回答
4

一个简单和通用的(虽然不是最佳的)解决方案可以通过该C-x ()机制。

首先在 Emacs 中打开两个窗格,其中 -say-top 一个被dired

  • o打开第二个窗格中的第一个文件。

然后就可以启动重复机制了:

  • 开始C-x (录制宏
  • C-x kreturn关闭缓冲区
  • 再做o一次回到原来的样子
  • down键进入下一个文件
  • 在底部窗格中o打开下一个文件
  • 结束C-x )

从那时起(在底部窗格中,在顶部窗格中),只做一个

  • C-x e (然后只有e在两者之间没有其他操作的情况下)

会自动

  • 关闭底部窗格文件,转到顶部窗格,向下到下一个文件,在底部窗格中打开它

可能有一种更具体的方法可以做到这一点,但是了解宏机制无论如何在 Emacs 中非常有帮助。

于 2013-11-11T15:00:36.997 回答
4

非常感谢所有这些答案。总结一下,我创建了以下解决方案(扩展“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

更改了三个键:

  1. C-o在另一个窗口中查看当前项目(可能创建一个)。
  2. n在另一个窗口中查看下一个项目。
  3. p在另一个窗口中查看上一个项目。

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.

于 2013-11-12T15:35:32.407 回答
3
  1. 加载冰柱

  2. 定义这个命令:

    (defun 我的查找文件 ()
      “像 `icicle-find-file',但 alt 操作临时查看文件。
    备用操作键,例如“CS-down”访问候选文件
    `view-mode' 并杀死最后一个这样查看的候选者的缓冲区。”
      (交互的)
      (let ((icicle-candidate-alt-action-fn
             (拉姆达(文件)
               (当(和我最后一次查看
                          (get-file-buffer my-last-viewed))
                 (kill-buffer (get-file-buffer my-last-viewed)))
               (setq my-last-viewed (缩写文件名文件))
               (查看文件文件)
               (选择框架集输入焦点
                  (窗口框架(活动迷你缓冲区窗口))))))
        (冰柱查找文件内容)))

    (defvar my-last-viewed nil
      “通过 `my-find-file' 的替代操作查看的最后一个文件。”)

那么你就可以:

  1. 使用M-x my-find-file(或将其绑定到一个键 - 例如,C-x C-f)。
  2. (可选)键入文件名的一部分,以限制匹配的名称。
  3. 可选择使用downup在文件名之间循环。
  4. 用于C-S-down按顺序访问下一个文件。
  5. 重复 #4 以按顺序查看其他文件。
  6. 重复 #2 或 #3 以查看其他文件集。
  7. RET以选择要访问或C-g取消的文件结束。

C-S-down当您查看下一个文件缓冲区时,您访问的每个文件缓冲区都被杀死了。您还可以混合C-downC-RET访问您不想自动杀死其缓冲区的文件。(如果您不想访问,请更改view-file为,这是只读的。)find-fileview-mode

[默认情况下,icicle-find-fileis 的替代操作icicle-alt-act-fn-for-type会提示您针对为该操作选择的特定候选者使用适合文件的操作。命令my-find-file只是替代了不同的替代动作功能(对于您选择的所有候选者)。]

另请参阅来自 help-gnu-emacs@gnu.org 的此线程。我想这和你的问题差不多。我的回复与我在这里的回复几乎相同,但也有其他人的回复也可能对您有所帮助。

于 2013-11-11T14:53:39.177 回答
2

尝试

M-x speedbar

这可能会吸引你

于 2013-11-11T13:48:17.257 回答
1

Another view-mode solution on top of ag-mode lists. I couldn't find a question for ag-mode, maybe this helps someone generalize a 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 a b)))

(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 "M-p") 'previous-view-current)
(define-key ag-mode-map (kbd "M-n") 'next-view-current)

This is the one thing I think Sublime does better than Emacs. Blasphemy, I know! I like the "q to exit" feel of view-mode, rather than timer-based solutions, and like scrolling around a previewed file. This snippet navigates to the line number found in the search results, optimizing for browsing speed.

Note about the code: I tried polyfilling vc-root-dir from Emacs 25, but it doesn't really make sense for ag-mode since ag-mode's buffer is outside the repo you're searching in. I ended up pulling the root dir from the top of the "ag search" buffer.

Early stages. Improvements welcome.

Demo

Edit: It works for ag-mode, not dired. Demo gif.

Credits: abo-abo, user2979331

于 2016-06-21T22:01:43.517 回答