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

时间:2016-02-29 13:21:28

标签: emacs elisp

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

我的shell历史记录如下:

% echo 1
% echo 2

我找到了包含命令的comint-dynamic-list-input-ring,但它在这样的反向排序表中

echo2       echo1

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

2 个答案:

答案 0 :(得分:1)

我认为您应该设置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))))

答案 1 :(得分:1)

非常感谢@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.")))