将 emacs shell 输入列表复制到缓冲区或文件

Copy emacs shell input list to a buffer or file

我有一个 emacs shell 缓冲区,我想将我输入的每个命令作为新的文本行保存在新的临时缓冲区中。

我的 shell 历史是这样的:

% echo 1
% echo 2

我找到了包含命令的 comint-dynamic-list-input-ring,但它是反向排序的 table,就像这样

echo2       echo1

我需要一个前向排序的时间顺序列表,最好是在一个临时缓冲区中,这样我就可以编辑缓冲区并保存到 .bash 文件或其他文件中。

我认为你应该设置 comint-input-ring-file-name 并使用 comint-write-input-ring 来保存你的命令:

(defun my-shell-buffers ()
  "Return the list of buffers with non-nil `comint-input-ring'."
  (let (ret)
    (dolist (b (buffer-list) ret)
      (with-current-buffer b
        (when comint-input-ring
          (push (buffer-name b) ret))))))
(defun my-edit-history (comint-buffer history-file)
  (interactive
   (list (completing-read "Comint buffer: "
                          (or (my-shell-buffers)
                              (error "No shell buffers"))
                          nil t nil nil
                          (and comint-input-ring (buffer-name)))
         (read-file-name "History file name: ")))
  (with-current-buffer comint-buffer
    (let ((comint-input-ring-file-name history-file))
      (comint-write-input-ring)
      (find-file comint-input-ring-file-name))))

非常感谢@sds

仅作记录,我自己的最新版本是:

(defun write-input-ring (filename)
  "Write shell input to FILENAME then visit FILENAME."
  (interactive "F")
  (let ((comint-input-ring-file-name filename))
    (comint-write-input-ring))

  (if (file-readable-p filename)
      ;; If the input ring was saved to a file, visit that file
      (find-file filename)
    ;; Else report that no input was saved
    (message "This buffer has no shell history.")))