I use org-mode to manage my to-do list with priorities and deadlines but inevitably I have multiple items without a specific deadline or scheduled date and that have the same priority. These appear in my agenda in the order in which they were added to my to-do list, but I’ll sometimes want to change that order. This can be done temporarily using
M-DOWN in the agenda view, but these changes are lost when the agenda is refreshed.
I came up with a two-part solution to this. The main part is a generic function to move the subtree at the current point to be the top item of all subtrees of the same level. Here is the function:
(defun bjm/org-headline-to-top () "Move the current org headline to the top of its section" (interactive) ;; check if we are at the top level (let ((lvl (org-current-level))) (cond ;; above all headlines so nothing to do ((not lvl) (message "No headline to move")) ((= lvl 1) ;; if at top level move current tree to go above first headline (org-cut-subtree) (beginning-of-buffer) ;; test if point is now at the first headline and if not then ;; move to the first headline (unless (looking-at-p "*") (org-next-visible-heading 1)) (org-paste-subtree)) ((> lvl 1) ;; if not at top level then get position of headline level above ;; current section and refile to that position. Inspired by ;; https://gist.github.com/alphapapa/2cd1f1fc6accff01fec06946844ef5a5 (let* ((org-reverse-note-order t) (pos (save-excursion (outline-up-heading 1) (point))) (filename (buffer-file-name)) (rfloc (list nil filename nil pos))) (org-refile nil nil rfloc))))))
This will move any to-do item to the top of all of the items at the same level as that item. This is equivalent to putting the cursor on the headline you want to move and hitting
M-UP until you reach the top of the section.
Now I want to be able to run this from the agenda-view, which is accomplished with the following function, which I then bind to the key
1 in the agenda view.
(defun bjm/org-agenda-item-to-top () "Move the current agenda item to the top of the subtree in its file" (interactive) ;; save buffers to preserve agenda (org-save-all-org-buffers) ;; switch to buffer for current agenda item (org-agenda-switch-to) ;; move item to top (bjm/org-headline-to-top) ;; go back to agenda view (switch-to-buffer (other-buffer (current-buffer) 1)) ;; refresh agenda (org-agenda-redo) ) ;; bind to key 1 (define-key org-agenda-mode-map (kbd "1") 'bjm/org-agenda-item-to-top)
Now in my agenda view, I just hit
1 on a particular item and it is moved permanently to the top of its level (with deadlines and priorities still taking precedence in the final sorting order).