Emacs:将shell命令应用于kill ring中的top项并且猛拉

时间:2013-03-28 20:58:03

标签: emacs

说我在Emacs中的剪贴板中有一些文本(杀死响铃)。我想设置一个键盘快捷键:

在对此文本应用某个shell命令后拉动文本:例如:

   printf $1 | sed 's@^\\@/samba/@;s@\\@/@g'

(在这种情况下$1将是剪贴板中的文字)

如何在Emacs中执行此操作?

3 个答案:

答案 0 :(得分:5)

除非shell命令始终相同,否则没有理由为此编写任何elisp:

C-y C-u M- | sed 's@^\\@/samba/@;s@\\@/@g' RET

答案 1 :(得分:3)

以下是通用shell-command-on-string

(defun shell-command-on-str (cmd &optional str)
  "Insert result of calling CMD with STR as input.

STR is current-kill if unspecified.
"
  (interactive (list (read-shell-command "Shell command on region: ")))
  (setq str (or str
                (current-kill 0)))
  (insert (with-temp-buffer
            (insert str)
            (shell-command-on-region (point-min) (point-max) cmd nil 'replace)
            (buffer-string))))

您可以定义自己的使用它的功能:

(defun my-sed-on-kill ()
  (interactive)
  (shell-command-on-str "sed 's@^\\@/samba/@;s@\\@/@g'"))

答案 2 :(得分:1)

要直接回答您的问题,您可以利用Emacs shell-command-on-region

(defun sed-yank ()
  "Yank the kill ring, then run sed on it"
  (interactive)
  (yank)
  (shell-command-on-region (point) 
                           (mark) 
                           "sed 's@^\\\\@/samba/@;s@\\\\@/@g'" 
                           (current-buffer)
                           t))

您可以在“sed ...”行中输入您想要的任何命令。问题是需要在elisp引号内转义反斜杠,因此原始\\变为\\\\

那就是说,除非你的shell脚本特别复杂,否则我怀疑你最好直接在Emacs中进行所有处理,而不是把它培养成sed。