Earlier in v1 when titles were basically notes, vs title with ID is a note, one could rename the title and the filename used to change. After v2 implementation when I change title I see the filename is still the same.
Can someone suggest on how to achieve this?
I am also asking about this and like to know if anyone has achieved this.
I wrote this function to update backlinks and file names on a node-name change. It’s messy and I haven’t tested it much, but maybe it’s a start:
(defun azr/org-roam-modify-title ()
"Modify title of org-roam current node and update all backlinks in roam database."
(unless (org-roam-buffer-p) (error "Not in an org-roam buffer."))
(let* ((old-title (org-roam-get-keyword "title"))
(ID (org-entry-get (point) "ID"))
(new-title (read-string "Enter new title: " old-title)))
(org-roam-set-keyword "title" new-title)
(let* ((new-slug (org-roam-node-slug (org-roam-node-at-point)))
(new-file-name (replace-regexp-in-string "-.*\\.org" (format "-%s.org" new-slug) (buffer-file-name)))
(new-buffer-name (file-name-nondirectory new-file-name)))
(rename-file (buffer-file-name) new-file-name 1)
(set-visited-file-name new-file-name)) ; I don't know why this last command is necessary. Getting it from here: https://stackoverflow.com/a/384346/2422698
;; Rename backlinks in the rest of the Org-roam database.
(let* ((search (format "[[id:%s][%s]]" ID old-title))
(replace (format "[[id:%s][%s]]" ID new-title))
(rg-command (format "rg -t org -lF %s ~/Org/roam/" search))
(file-list (split-string (shell-command-to-string rg-command))))
(dolist (file file-list)
(let ((file-open (get-file-buffer file)))
(while (search-forward search nil t)
Thanks @arozbiz .
I just have a test and it works!
BTW, I adjust the rg-command a little bit, to make ‘org-roam-directory’ not hardcoded.
(rg-command (format “rg -t org -lF %s ~/Org/roam/” search)
(rg-command (format (concat "rg -t org -lF %s " org-roam-directory) search)
Awesome guys! thanks a lot
My solution to this is
(defun org-rename-to-new-title ()
(is-roam-file (org-roam-file-p old-file))
(slug (org-roam-node-slug file-node))
(new-file (expand-file-name (concat slug ".org")))
(different-name? (not (string-equal old-file new-file))))
(rename-file old-file new-file)
(note using Doom’s
add-hook! rather than the standard
add-hook which is slightly different)
This renames the file, but does not rename link references (I don’t like doing this)
set-visited-file-name assigns the buffer to the new file so that writing the buffer will write to the correct new file.