From an org list item, I want a function that I can bind to a shortkey or an org-speed shortcut, to be able to launch a shell command with the org list item text as parameter. Then I want to add tag to the item

* item one
* item I two  ← caret is here 
* item three

I want to launch a command shell like

./my_command "item two" ← this is returning a short uuid 

then adding a tag

* item one
* item two           :b7acda1e:  ← the tag added from previous command line
* item three

I'm not a elisp developer, but they told me `org-element could help here


There are three things to do, so we write a function for each:

  • get the headline text
  • run the command with the headline text as argument
  • add the result as a tag to the headline
(defun ndk/headline-text ()
  (let* ((el (org-element-at-point))
         (typ (org-element-type el)))
    (when (eq typ 'headline)
      (plist-get (cadr el) :raw-value))))

(defun ndk/run-my-command (arg)
  (unless (null arg)
    (shell-command (concat "/tmp/command" " " arg))
    (with-current-buffer shell-command-buffer-name
      (buffer-substring-no-properties  (point-min) (point-max)))))

(defun ndk/insert-tag (tag)
  (unless (null tag)
    (let ((tags (org-get-tags)))
      (unless (member tag tags)
         (org-set-tags (cons tag tags))))))

(defun ndk/do-it ()
  (ndk/insert-tag (ndk/run-my-command (ndk/headline-text))))

The ndk/do-it command then just composes the three functions. You can bind it to a key if you want:

(define-key org-mode-map "C-c t" #'ndk/do-it)

You will need to customize the path to your command (/tmp/command above).

The functions take some pains to act reasonably even under unreasonable usage: they all return nil on error and the functions ndk/run-my-command and ndk/insert-tag do nothing if their argument is nil. Moreover, ndk/insert-tag checks whether the tag exists already before adding it.

  • thank you for the answer, I'll test tomorrow and validate it
    – taharqa
    Jun 5 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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