Suppose I have a latex file like





Some text


Some text 


and so on...


Now I tried to create a function which opens the "nearest" image above the cursor:

(defun my-show-img ()
    (let (imgfile)
      (while (not (looking-at "\\\\includegraphics\\[?.*\\]?{\\(?1:.*\\(\\.png\\|\\.jpg\\)\\)}"))
    (forward-char -1))
      (setq imgfile (match-string-no-properties 1))
      (if (file-exists-p imgfile)
                    ;(message imgfile)
      (async-shell-command (concat "eog -w " imgfile))

For example if the cursor is somewhere between \includegraphics{image3.jpg} and \includegraphics{image2.jpg} it will open image2.jpg.

Now I have several problems with this:

  1. When executing it, it opens a new (empty) buffer *Async Shell Command*. How can I prevent this?
  2. If I execute it a second time (maybe cursor on another place, for example between \includegraphics{image2.jpg} and \includegraphics[width=\textwidth]{image1.jpg}), it asks me "A command is running in the default buffer. Use a new buffer?". How can I prevent this message?

At the end I want to be able the following: Call something like enable-img-preview then it opens eog with the "nearest" image before the cursor (just like above), however if I move the cursor (or click with the mouse somewhere) it should automatically update the showing image with the image thats now the "nearest" before the cursor (without any messages or other buffers, see above). This behaviour should end when calling another function say disable-img-preview.

How can I implement this feature? Since this probably needs a background process which watches the cursor position, it would be great to find a way which doesn't slow down emacs or takes much resources.

Other improvement suggestions for my source code are welcome.


1 Answer 1


Well, let us have a look at the docs: C-h f async-shell-command.

You can configure ‘async-shell-command-buffer’ to specify what to do when the buffer specified by ‘shell-command-buffer-name-async’ is already taken by another running shell command.

This answers question 2.

To run COMMAND without displaying the output in a window you can configure ‘display-buffer-alist’ to use the action ‘display-buffer-no-window’ for the buffer given by ‘shell-command-buffer-name-async’.

This answers question 1.

In Elisp, you will often be better served by calling ‘start-process’ directly, since it offers more control and does not impose the use of a shell (with its need to quote arguments).

This suggests a more robust approach.

When in doubt: ask emacs!

  • Thanks, using start-process avoids both problems. Do you have also a solution to the third part of the question?
    – Julia
    Sep 4, 2022 at 8:16
  • Maybe add something to post-command-hook? Sep 4, 2022 at 8:25
  • Can you give some more details?
    – Julia
    Sep 29, 2022 at 12:30
  • Not really: I was just speculating. The idea would be that after every keystroke or mouse-click (each fires a command), a function in the hook would inspect the cursor position and decide whether to do something with eog or whatever. In any case, this is a topic for a different question. Sep 29, 2022 at 22:12

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.