Search code examples
emacselisp

Copy emacs shell input list to a buffer or file


I have an emacs shell buffer and would like to save each command that I have input as a new line of text in a new temporary buffer.

My shell history is something like:

% echo 1
% echo 2

I have found comint-dynamic-list-input-ring which contains the commands but it's in a reverse-sorted table like this

echo2       echo1

I need a forward-sorted chronological list, ideally in a temporary buffer so I can edit the buffer and save to a .bash file or what have you.


Solution

  • I think you should set comint-input-ring-file-name and use comint-write-input-ring to save your commands:

    (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))))