I have a python script ss.py that gets a screenshot from a connected device and puts into particular dir. From terminal I run it with the target path as an arg:

$ python ss.py {document-name}/{image-name}

Then I have to insert some text into my document to reference it.

.. image:: /img/{document-name}/{image-name}.*
  :alt: {Some alt text here}

I want to write a command that will accept the image name as an argument, take the doc name from the current buffer (minus file extension) and then put the image directive into the current buffer, leaving me and my cursor at the alt text, so I can keep writing there. And I want to bind it to some key (I don't what is commonly unused/available.)

(I am very new to emacs. So I don't even know where to start with this.)

NB: In case it matters, I need to run the Python command using python3, which normally means I use a virtualenv in a bash shell. Also, in case it matters I am using emacs 25 on Mac OS X.

1 Answer 1


Figured it out myself...

(defun odk-ss (img-name)
  "Takes a screenshot of an attached Android device, saves it to /img/ and then inserts a referen\
ce to it in an rst document"
  (interactive "sImage name: ")
  (insert-before-markers (format ".. image:: /img/%s/%s.* \n  :alt:" (file-name-base) img-name)
  (shell-command (format "python ss.py %s/%s" (file-name-base) img-name)) ; shell command goes he\


(global-set-key (kbd "M-.") 'odk-ss)
  • In that case you should accept your own answer so that other people will be sure that your question was answered to satisfaction
    – Joafigue
    Aug 11, 2017 at 6:41
  • Yes. But StackExchange won't let me accept my own answer for several days. Aug 11, 2017 at 16:08
  • Several days have elapsed :)
    – phils
    Mar 9, 2018 at 4:52
  • 1
    You probably want to use shell-quote-argument when generating your shell command, and I wouldn't recommend clobbering M-. which has a standard purpose; you can safely use any C-c <letter> sequence for your own purposes (where <letter> is any lower- or upper-case letter), as those are reserved for end-users.
    – phils
    Mar 9, 2018 at 4:56

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.