73

vi[m] には、シェル コマンド (sort や indent など) を介して!テキストをパイプ処理し、フィルター処理されたテキストをバッファーに戻すコマンドがあります。emacsに同等のものはありますか?

4

3 に答える 3

122

地域を選択して「CuM-|」と入力できます。command RET'であり、shell-command-on-regionの対話型プレフィックス引数により、同じバッファー内のコマンド出力で領域が置き換えられます。

于 2008-10-16T00:27:06.460 に答える
18

私は数年前にこれを書きました、それはあなたを助けるかもしれません:

(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)
于 2008-10-16T00:58:28.707 に答える
10

レイト編集:賛成票に感謝する限り、Jurtaの答えが道です。そして、グレッグのハックは私のハックよりも優れています。

何か価値があるかもしれないので、これの残りはここに残しておきますが...


M-x shell-command-on-regionM-|、デフォルトでバインドされているようです。


これは、Rohit が要求したこととまったく同じではありません。を使用すると、(引数を nil 以外に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)
  )

そして、コメントで私が言っているように、これは非常にemacsyなことではないことに注意してください。しかし、私はそれがうまくいくと思います。


地域の選択方法がわからない読者向け:

  1. 「ポイント」(現在のカーソル位置)をリージョンの一方の端に移動し、 を使用C-spaceして「マーク」をアクティブにします
  2. ポイントをリージョンのもう一方の端に移動します
  3. 完了したら、コマンドを呼び出します
于 2008-10-15T22:43:29.977 に答える