当前位置:  开发笔记 > 编程语言 > 正文

通过Emacs中的shell命令过滤文本

如何解决《通过Emacs中的shell命令过滤文本》经验,为你挑选了3个好方法。

在vi [m]中有一个!命令,它允许我通过shell命令管道文本 - 比如排序或缩进 - 并将过滤后的文本放回缓冲区.在emacs中是否有相同的内容?



1> link0ff..:

您可以选择一个区域并输入"Cu M- |" 命令RET',由于shell-command-on-region的交互前缀参数,它在同一缓冲区中用命令输出替换该区域.


Jurta获胜.给予好评.我发誓,每当我想我开始了解这个节目时,有人会出现并告诉我,我是初学者.
Rohit,你可以通过打开Emacs手册(`ch idm Emacs RET')找到它在Emacs中的文档,并在索引中查找命令名 - `i shell-command-on-region RET'.
当Emacs Regexp不太适合使用“ perl pie”进行正则表达式搜索和替换时,这非常有用。`铜M- | perl -pi -e's / pattern / replace / g'RET`。Evil也支持Vim命令(`:%!perl -pi -e ... RET`)。

2> Greg Mattes..:

几年前我写了这篇文章,它可能对你有帮助:

(defun generalized-shell-command (command arg)
  "Unifies `shell-command' and `shell-command-on-region'. If no region is
selected, run a shell command just like M-x shell-command (M-!).  If
no region is selected and an argument is a passed, run a shell command
and place its output after the mark as in C-u M-x `shell-command' (C-u
M-!).  If a region is selected pass the text of that region to the
shell and replace the text in that region with the output of the shell
command as in C-u M-x `shell-command-on-region' (C-u M-|). If a region
is selected AND an argument is passed (via C-u) send output to another
buffer instead of replacing the text in region."
  (interactive (list (read-from-minibuffer "Shell command: " nil nil nil 'shell-command-history)
                     current-prefix-arg))
  (let ((p (if mark-active (region-beginning) 0))
        (m (if mark-active (region-end) 0)))
    (if (= p m)
        ;; No active region
        (if (eq arg nil)
            (shell-command command)
          (shell-command command t))
      ;; Active region
      (if (eq arg nil)
          (shell-command-on-region p m command t t)
        (shell-command-on-region p m command)))))

我发现这个功能非常有用.如果你发现它也很有用,我建议将它绑定到一些功能键以方便使用,我个人使用F3:

(global-set-key [f3] 'generalized-shell-command)



3> dmckee..:

延迟编辑:尽管我很欣赏赞成票,但是Jurta的答案是要走的路.格雷格的黑客比我的还要整洁.

我会在这里留下其余部分,因为它可能是值得的,但......


M-x shell-command-on-region,似乎M-|默认绑定.


我发现这并不完全符合Rohit所要求的.使用C-h f shell-command-on-region显示在命令的非交互版本中可以使用所需的行为(通过将参数设置replace为非零).我们应该能够编写一个包装器来执行此操作.

试试这个(将其加载*scratch*并运行M-x eval-buffer,如果有效,将其复制到.emacs文件中):

(defun shell-command-on-region-replace (start end command)
  "Run shell-command-on-region interactivly replacing the region in place"
  (interactive (let (string) 
         (unless (mark)
           (error "The mark is not set now, so there is no region"))
         ;; Do this before calling region-beginning
         ;; and region-end, in case subprocess output
         ;; relocates them while we are in the minibuffer.
         ;; call-interactively recognizes region-beginning and
         ;; region-end specially, leaving them in the history.
         (setq string (read-from-minibuffer "Shell command on region: "
                            nil nil nil
                            'shell-command-history))
         (list (region-beginning) (region-end)
               string)))
  (shell-command-on-region start end command t t)
  )

请注意,正如我在评论中所说,这不是一件非常麻烦的事情.但我认为它有效.


对于任何不知道如何选择地区的读者:

    将"点"(当前光标位置)移动到区域的一端,并用于C-space激活"标记"

    将该点移动到该区域的另一端

    完成后,调用命令

推荐阅读
贾志军
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有