【问题标题】:Emacs shell command on buffer缓冲区上的 Emacs shell 命令
【发布时间】:2013-04-07 23:33:46
【问题描述】:

我想设置一个相当于标记整个缓冲区的函数,并运行C-u M-| 以提示输入命令,将缓冲区传递给命令并用输出替换缓冲区。然后可能将其设置为 shift-f5 或其他东西。

我只知道这个:

(defun shell-command-on-buffer ()
  (interactive)
  (mark-whole-buffer))

剩下的怎么办?

【问题讨论】:

    标签: emacs elisp


    【解决方案1】:

    这对我有用:

    (defun shell-command-on-buffer (command)
      (interactive "sShell command on buffer: ")
      (shell-command-on-region (point-min) (point-max) command t))
    

    【讨论】:

    • 谢谢,我试过了。这为我打开了一个新的缓冲区——理想情况下,我希望它替换当前的缓冲区,如 C-u M-|是的。
    • 啊,抱歉,我最初的建议与shell-command-on-region 具有相同的 C-u 行为。编辑以更好地满足您的要求
    • 恐怕不走运 - 它会提示我输入命令,但会给出错误数量的参数错误。
    • 奇怪.. 我刚刚开始了一个没有自定义的新 emacs (-q),它对我有用。什么版本的emacs?
    • 24.2.1.完整的错误是:错误数量的参数:(lambda nil(缓冲区上的交互式“sShell命令:”)(shell-command-on-region(point-min)(point-max)command t)),1
    【解决方案2】:

    这个的优点是使用“shell 命令”minibuffer 历史而不是一般的 minibuffer 历史。

    (defun shell-command-on-buffer ()
      (interactive)
      (shell-command-on-region (point-min) (point-max) (read-shell-command "Shell command on buffer: ") t))
    

    【讨论】:

    • 请注意 read-shell-command 是在 GNU Emacs 23.1 版本中引入的。如果您需要与旧版本兼容,那么您将不想使用它,或者您需要在尝试调用它之前添加检查 read-shell-command 是否存在的代码。
    【解决方案3】:

    @killdash9 的答案的增量改进,即:

    1. 确实替换了缓冲区内容(并且不会将输出附加到缓冲区的头部)
    2. 努力恢复光标位置。

    用 emacs 27.1 测试

    (defun shell-command-on-buffer ()
      (interactive)
      (let ((line (line-number-at-pos)))
        ;; replace buffer with output of shell command
        (shell-command-on-region (point-min) (point-max) (read-shell-command "Shell command on buffer: ") nil t)
        ;; restore cursor position
        (goto-line line)
        (recenter-top-bottom)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 2018-07-31
      • 2012-01-25
      相关资源
      最近更新 更多