代码之家  ›  专栏  ›  技术社区  ›  Rohit

通过Emacs中的shell命令筛选文本

  •  64
  • Rohit  · 技术社区  · 16 年前

    ! command which lets me pipe text 通过shell命令(如sort或indent)将过滤后的文本返回到缓冲区。emacs中是否有一个等价物?

    3 回复  |  直到 9 年前
        1
  •  120
  •   link0ff    16 年前

    您可以选择一个区域并键入“C-u M-| command RET”,由于区域上的shell命令的交互前缀参数,它将该区域替换为同一缓冲区中的命令输出。

        2
  •  17
  •   Greg Mattes    16 年前

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

    (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
  •  10
  •   dmckee --- ex-moderator kitten    16 年前

    我会把剩下的留在这里,因为它可能值点钱,但是。。。


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


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

    1. 将“点”(当前光标位置)移动到区域的一端,然后使用 C-space
    2. 将点移动到区域的另一端
    3. 完成后,调用命令