emacs - save current buffer list to a text file

2019-03-26 01:21发布

Quite often I need to get a simple text copy of my currently opened files. The reasons are usually:

  • I want to send the list to a colleague
  • I want to document whatever I am working on (usually in an org document)
  • I want to act on one of my currently opened files, on the shell. I need to copy-paste the pathname for that.

The fact is that the usual buffer-menu or list-buffers provide a convenient menu to navigate the opened buffers, but are very inconvenient to copy-paste to the the terminal the names of the opened files, or to perform any of the actions mentioned above. For example: I can not double-click in a line to select the full path-name, and I can not use the kill/yank emacs sequence to copy around the path-name.

Summary: I would like a way to export to a text file (or to a new buffer) the list of opened files, without other data; no file size, mode, or any other emacs metadata.

Is there a command for that? An extra package I can install?

EDIT

Adding solution by Trey Jackson, modified to provide some feedback of what has been done:

(defun copy-open-files ()
  "Add paths to all open files to kill ring"
  (interactive)
  (kill-new (mapconcat 'identity 
                       (delq nil (mapcar 'buffer-file-name (buffer-list))) 
                       "\n"))
  (message "List of files copied to kill ring"))

3条回答
Deceive 欺骗
2楼-- · 2019-03-26 01:35

This command will do the job for you:

(defun copy-open-files ()
  "Add paths to all open files to kill ring"
  (interactive)
  (kill-new (mapconcat 'identity 
                       (delq nil (mapcar 'buffer-file-name (buffer-list))) 
                       "\n")))
查看更多
我想做一个坏孩纸
3楼-- · 2019-03-26 01:37

You can change the mode of your *Buffer List* buffer. By default, it will be in mode Buffer Menu, but changing it to text-mode or fundamental-mode will remove all the special behavior allowing you to cut and paste from it just like a regular buffer. The metadata can easily be chopped off with delete-rectangle.

Alternatively, you can access the buffer list programmatically with elisp:

(dolist (buffer (buffer-list))
  (when (buffer-file-name buffer)
    (insert (buffer-file-name buffer) "\n")))
查看更多
萌系小妹纸
4楼-- · 2019-03-26 01:38

You certainly should be able to copy and yank from the buffer list.

e.g. copy everything with C-xhM-w and then yank into a new buffer for editing.

查看更多
登录 后发表回答