Emacsのシェルコマンドでテキストをフィルタリングする
-
03-07-2019 - |
解決
地域を選択して、「C-u M- |コマンドRET 'を使用すると、shell-command-on-regionの対話型プレフィックス引数により、領域が同じバッファー内のコマンド出力に置き換えられます。
他のヒント
これは数年前に書いたもので、あなたに役立つかもしれません:
(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)
後期編集:賛成票に感謝しますが、Jurtaの答えが道です。グレッグのハックは私のものよりもすっきりしています。
何かの価値があるかもしれないので、これの残りをここに残しますが、...
Mxシェルコマンドオンリージョン
。 M- |
にバインドされているようです。デフォルト。
これは、Rohitが要求したものとまったく同じではないようです。 Ch f shell-command-on-region
を使用すると、コマンドの非インタラクティブバージョンで目的の動作が利用できることがわかります(引数 replace
を非nilに設定することにより) )。これを行うラッパーを作成できるはずです。
これを試してください( * 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
を使用して" mark" をアクティブにします
- ポイントを領域のもう一方の端に移動する
- 完了したら、コマンドを呼び出します
所属していません StackOverflow