【问题标题】:How can I open a long list of files from within emacs如何从 emacs 中打开一长串文件
【发布时间】:2019-06-04 14:48:36
【问题描述】:

我有一长串文件(完整的路径名,一个在自己的行上,在一个文本文件中),我想将所有这些文件打开到 emacs 缓冲区中,这样我就可以使用 multi-occur -in-matching-buffers 在这些文件中导航。

如何在 emacs 中打开文件列表?列出的文件位于任意文件夹中并具有任意文件名。即,路径和名称没有正则模式,所以我不是在寻找特定的正则表达式来匹配下面的示例。

我不想在 emacs 命令行调用中执行此操作,因为我通过单击图标在 Windows 上运行 emacs,而且我想保持打开我已经打开的其他缓冲区(文件)。

我能够创建一个自定义 elisp 函数(函数中文件名列表的硬编码),如下(简短示例)。

(defun open-my-files ()
  "Open my files"
  (interactive)
  (find-file "c:/files/file1.txt")
  (find-file "c:/more_files/deeper/log.log")
  (find-file "c:/one_last_note.ini")
)

我可以将该 elisp 放在缓冲区中,然后全选,然后 eval-region,然后使用 M-x open-my-files 执行函数。

但是,如果 elisp 会从包含列表的文本文件中读取文件列表,那对我来说会更有效率。

【问题讨论】:

  • C-x C-f "c:/files/file*.txt*" 或者如果您在该目录中,只需 C-x C-f "file*.txt*"
  • 对不起,在 elisp 示例解决方案中,我在同一个文件夹中显示了 3 个文件,但长文件列表包含来自任意文件夹的文件。即,这是一个很长的逐项列表,文件名也是任意的。我只是在证明我可以将其作为一个 elisp 函数来完成,尽管需要进行编辑。
  • 同意。我编辑了这个问题,试图让它现在更清楚。谢谢。
  • 如果你想打开一堆文件只是为了“搜索/出现”,你可以使用 the_silver_searcher (with helm-ag) 来搜索未访问过的文件。

标签: emacs


【解决方案1】:

我已经得出以下解决方案。类似于 James Anderson 提出的答案,但将 re-search-forward 替换为 thing-at-point,并根据其他一些参考资料进行了一些其他更改。

  (defun open-a-bunch-of-files ()
    "Open a a bunch of files, given a text file containing a list of file names"
    (interactive)
    (setq my_filelist (completing-read "my_filelist: " nil nil nil))
    (with-temp-buffer
      (insert-file-contents my_filelist)
      (goto-char (point-min))
      (while (not (eobp))
        (find-file-noselect (replace-regexp-in-string "\n$" "" (thing-at-point 'line t)))
        (forward-line)
      )
    )
  )

参考资料:

https://emacs.stackexchange.com/questions/19518/is-there-an-idiomatic-way-of-reading-each-line-in-a-buffer-to-process-it-line-by

Grab current line in buffer as a string in elisp

How do I delete the newline from a process output?

【讨论】:

    【解决方案2】:

    这似乎在我的机器上工作:

    (defun open-a-bunch-of-files (filelist)
      (with-temp-buffer
        (insert-file-contents filelist)
        (goto-char (point-min))
        (let ((done nil))
          (while (not done)
            (if (re-search-forward "^\\([a-z_A-Z:\/.0-9]+\\)$" nil t nil)
                (find-file-noselect (match-string 1))
              (setf done t))))))
    
    (open-a-bunch-of-files "./filelist.txt")
    

    您可能需要使用正则表达式(在 unix 文件系统上测试)。还有它的emacs,所以可能有人会指出更好的方法。加载的缓冲区不会故意设置为当前缓冲区。

    【讨论】:

    • let 表单看起来有点傻,当它找不到更多匹配项时让re-search-forward 停止。
    猜你喜欢
    • 1970-01-01
    • 2014-08-29
    • 2013-10-17
    • 2021-11-27
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多