Roh*_*hit 65 emacs elisp text-editor
在vi [m]中有一个!
命令,它允许我通过shell命令管道文本 - 比如排序或缩进 - 并将过滤后的文本放回缓冲区.在emacs中是否有相同的内容?
lin*_*0ff 111
您可以选择一个区域并输入"Cu M- |" 命令RET',由于shell-command-on-region的交互前缀参数,它在同一缓冲区中用命令输出替换该区域.
Gre*_*tes 17
几年前我写了这篇文章,它可能对你有帮助:
(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)))))
Run Code Online (Sandbox Code Playgroud)
我发现这个功能非常有用.如果你发现它也很有用,我建议将它绑定到一些功能键以方便使用,我个人使用F3
:
(global-set-key [f3] 'generalized-shell-command)
Run Code Online (Sandbox Code Playgroud)
延迟编辑:尽管我很欣赏赞成票,但是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)
)
Run Code Online (Sandbox Code Playgroud)
请注意,正如我在评论中所说,这不是一件非常麻烦的事情.但我认为它有效.
对于任何不知道如何选择地区的读者:
C-space
激活"标记" 归档时间: |
|
查看次数: |
13957 次 |
最近记录: |