Does renaming title no longer renames the filename?

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."))
    (save-some-buffers t)
    (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 "" new-slug) (buffer-file-name)))
             (new-buffer-name (file-name-nondirectory new-file-name)))
	(rename-buffer new-buffer-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:
      ;; 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)))
	    (find-file file)
            (while (search-forward search nil t)
              (replace-match replace))
            (unless file-open
1 Like

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)

Thank you!

Awesome guys! thanks a lot

My solution to this is

(add-hook! 'after-save-hook
           (defun org-rename-to-new-title ()
                 ((old-file (buffer-file-name))
                  (is-roam-file (org-roam-file-p old-file))
                  (file-node (save-excursion
                               (goto-char 1)
                  (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-buffer new-file)
               (rename-file old-file new-file)
               (set-visited-file-name new-file)
               (set-buffer-modified-p nil))))

(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)

@arozbiz, the set-visited-file-name assigns the buffer to the new file so that writing the buffer will write to the correct new file.

1 Like

This work great for me. one little thing: from a UX perspective, I found it confusing that after renaming #+title and hitting C-x C-s the minibuffer prints a message saying that the buffer has been saved to /path/to/org-roam/ (even though it actually saves it to /path/to/org-roam/ as one would expect):

Saving file /path/to/org-roam/
Wrote /path/to/org-roam/

I replaced the last line of code with the following two lines, which seems to fix the issue

(set-buffer-modified-p t)

After hitting C-x C-s the minibuffer now prints

Saving file /path/to/org-roam/
Wrote /path/to/org-roam/
Saving file /path/to/org-roam/
Wrote /path/to/org-roam/

Disclaimer: I am new to elisp, so please correct me if I got anything wrong. Or if there is a better way to improve this.