简体   繁体   English

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

[英]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. 我有一个emacs shell缓冲区,想将我输入的每个命令另存为新的一行文本到新的临时缓冲区中。

My shell history is something like: 我的shell历史是这样的:

% 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 我发现comint-dynamic-list-input-ring包含命令,但它在这样的反向排序表中

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. 我需要一个按时间顺序排序的列表,最好是在一个临时缓冲区中,以便我可以编辑该缓冲区并保存到.bash文件或您拥有的文件中。

I think you should set comint-input-ring-file-name and use comint-write-input-ring to save your commands: 我认为您应该设置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))))

Thanks so much to @sds 非常感谢@sds

Just for the record, my own latest version is: 仅作记录,我自己的最新版本是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM