4

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.

2
  • 1
    +1 for the "I can do everything in Emacs" attitude! :-) Commented Jul 27, 2011 at 11:39
  • M-| is shell-command-on-region which pipes the current region into a command. That won't work. Victor's solution should solve your problem. Commented Jul 27, 2011 at 13:22

3 Answers 3

5

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))))))
Sign up to request clarification or add additional context in comments.

1 Comment

You probably also want to apply shell-quote-argument to the second and third arguments to format, or else be careful never to call this function when the region text includes ;rm -rf /.
5

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

See xargs.

Comments

3

Victor's answer is a good one for the question you asked, but in your specific case you might consider using M-x ffap (find-file-at-point). This will give you a dired buffer for the /home directory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.