Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass text as argument to external program using emacs

Tags:

emacs

Let's say I have this text in a file:

/home is where the heart is.

If for example, I select the /home text, using C-spc, is there a way of sending it to ls, so that in the end if will execute ls /home? M-| does not work.

like image 647
Senthess Avatar asked Dec 12 '22 10:12

Senthess


2 Answers

As far as I know, there is no way to do that in Emacs directly. But everyting is possible with help of elisp:

(defun region-as-argument-to-command (cmd)
  (interactive "sCommand: ")
  (shell-command
   (format
    "%s %s"
    cmd
    (shell-quote-argument
     (buffer-substring (region-beginning)
                       (region-end))))))
like image 99
Victor Deryagin Avatar answered Jan 12 '23 13:01

Victor Deryagin


Try M-| xargs ls. That is, pass "xargs ls" as the shell command on the region selected.

See xargs.

like image 28
huaiyuan Avatar answered Jan 12 '23 13:01

huaiyuan