【问题标题】:Copy emacs shell input list to a buffer or file将 emacs shell 输入列表复制到缓冲区或文件
【发布时间】:2023-03-12 06:17:01
【问题描述】:

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

我的 shell 历史是这样的:

% echo 1
% echo 2

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

echo2       echo1

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

【问题讨论】:

    标签: emacs elisp


    【解决方案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))))
    

    【讨论】:

    • 太好了,我可以创建一个小自定义函数来提示输入文件名,保存输入环,然后在缓冲区中找到该文件进行编辑。
    【解决方案2】:

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

    【讨论】:

    • 当您在非comint 缓冲区中调用它时,您希望它做什么?
    • 我想也许它可以(message "This buffer has no shell history") 。但是,我喜欢当前的解决方案,而且效果很好,所以我只是好奇它是如何完成的。非常感谢您昨天指出前进的方向。
    • 哇,谢谢。我认为(when c 行中有错字。也许这应该是(when comint-input-ring 。像这样的答案是 elisp 新手学习的最佳方式。
    猜你喜欢
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    • 2014-02-26
    • 2014-04-27
    • 2010-11-16
    • 1970-01-01
    相关资源
    最近更新 更多