2

Consider a buffer holding the following contents:

foo
...
some text /foo/bar/hello.csv𝙸
...
bar

where 𝙸 represents the location of cursor.

I would like to have an interactive function that automatically calls <command> $PATH on the shell, where $PATH is the path immediately preceding 𝙸 (it can do this by finding the first /).

How could I approach this problem in elisp?

I have seen packages like Helm being able to do something similar (e.g. C-x C-f on cursor above would openhelm-find-files on that location)

2 Answers 2

4

One way to achieve this is by using thing-at-point and shell-command:

(defun my-open-file-at-point ()
  (interactive)
  (shell-command (format "open %s" (thing-at-point 'filename))))
2

If you use Icicles then you can use M-. to insert text at or near the cursor into the minibuffer anytime.

So for example to run shell command my-shell-command, passing it the filename (or whatever) at point you would type this:

M-x shell-command my-shell-command M-. RET

When you hit M-. the thing at or near point is inserted, so the minibuffer content after the M-x prompt becomes shell-command /foo/bar/hello.csv.


Here is the doc string for M-.:

icicle-insert-string-at-point is an interactive compiled Lisp function
in `icicles-mcmd.el'.

(icicle-insert-string-at-point &optional ARG)

Insert text at the cursor into the minibuffer.
Each time this command is called, some text at or near the cursor is
inserted into the minibuffer.  One of two things happens, depending on
the value of option `icicle-default-thing-insertion' and whether or
not you use `C-u'.

See the doc for option `icicle-thing-at-point-functions' for a
complete description of its behavior.  What follows is an overview.

`icicle-thing-at-point-functions' is a cons of two parts - call them
ALTERNATIVES and FORWARD-THING.

If ALTERNATIVES is not nil and one of the following is true:
 - FORWARD-THING is nil
 - the value of `icicle-default-thing-insertion' is `alternatives' and
   you have not used plain `C-u' in this series of `M-.'
 - the value of `icicle-default-thing-insertion' is `more-of-the-same'
   and you have used plain `C-u' in this series of `M-.'
then the next function in ALTERNATIVES is used to retrieve the text to
be inserted.

If FORWARD-THING is not nil and one of the following is true:
 - ALTERNATIVES is nil
 - the value of `icicle-default-thing-insertion' is `more-of-the-same'
   and you have not used `C-u' in this series of `M-.'
 - the value of `icicle-default-thing-insertion' is `alternatives' and
   you have used `C-u' in this series of `M-.'
then function FORWARD-THING is used to retrieve the text to be
inserted.

If you use a numeric prefix arg (not just plain `C-u'), the behavior
is as follows.

* If a function in ALTERNATIVES is used (see above), then the text
  that is grabbed at or near point is read as a Lisp sexp and
  evaluated, and the value is inserted instead of the grabbed text.

  Yes, this means you need to know when the particular ALTERNATIVES
  function that you want is coming up next, and use, say, `C-9' just
  before hitting `M-.' for that alternative.  So if, e.g., you want to
  evaluate the active region and insert the value, then you use
  `M-. C-9 M-.', since it is the second `M-.' that grabs the region.

* If the FORWARD-THING is being used, then the prefix arg determines
  the number of things to grab, and the direction of grabbing.: A
  negative argument grabs text to the left of the cursor; a positive
  argument grabs text to the right.

You can use this command only from the minibuffer (`M-.').
1
  • This actually sounds amazingly handy because it's so generic.
    – PythonNut
    Commented Feb 4, 2016 at 3:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.