1

I store a property MyValue, in property drawers for a lot of headlines and need to update it regularly.

Instead of typing C-c C-x p MyValue 0 <RET> on each heading, I would like to have a custom key binding to change the value of this specific property and bind it e.g. to F7.

So pressing F7 is equivalent to C-c C-x p MyValue and I just have to set the value of MyValue to another numerical value.

Minimal example:

* demo 
:PROPERTIES:
:MyValue: 0
:END:
* example
:PROPERTIES:
:MyValue: 1
:END:

1 Answer 1

3

This is a two-part question: first, we need to create the custom behavior, then, we need to bind it to a key.

C-c C-x p runs the command org-set-property by default. The source code tells us it takes two arguments, property and value, but if called interactively, it'll prompt for both. So this tells us we can simply do something like this:

(defun gnumon-update-myvalue ()
  "Change myvalue to something else"
  (interactive)
  (let ((new-value (read-string "New value for MyValue? ")))
    (org-set-property "MyValue" new-value)))

Binding the function to a key needs to be done a little carefully, but since the help for the original keybinding told us that the keymap we use is org-mode-map, let's just reuse that one:

;; for emacs 29 and later
(keymap-set org-mode-map "<f7>" 'gnumon-update-myvalue)
;; for versions earlier than emacs 29
(define-key org-mode-map (kbd "<f7>") 'gnumon-update-myvalue)

And voila! F7 will trigger the function and let you update the value of the property for the heading at point.

7
  • 1
    Thank you @Trevoke! The function works perfectly! I get an error however when binding it to a key: Symbol's function definition is void: keymap-set
    – GNUmon
    Jul 12 at 1:55
  • OP: keymap-set is only available in recent Emacs versions. Just use define-key (with kbd) instead. Perhaps @trevoke will show you that (equivalent) code as well.
    – Drew
    Jul 12 at 3:04
  • @GNUmon added code for a keybinding for versions of emacs before 28.
    – Trevoke
    Jul 12 at 3:50
  • Thank you for your help! It works with define-key. I'm not sure why it isn't working with keymap-set though. I'm using GNU Emacs 28.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.37, cairo version 1.16.0) of 2023-05-14, modified by Debian
    – GNUmon
    Jul 12 at 12:47
  • 1
    keymap-set is introduced in Emacs 29.
    – Ian
    Jul 12 at 13:57

Your Answer

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

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