Skip to content

ObsidianRename does not replace the id of renamed file #688

Open
@monologconnor

Description

@monologconnor

🐛 Describe the bug

in file 20240820-old.md, the original id might be 20240820-old:

---
id: 20240820-old
aliases: []
tags: []
---

if i run the command :ObsidianRename new, the filename of current file would be changed to new.md and all references from other notes will be replaced to [[new]], however, the id property of current file remains 20240820-old:

---
id: 20240820-old
aliases: []
tags: []
---

And if then I would like to run :ObsidianRename 20240820-old for changing the name back to 20240820-old, plugin would not allow me to to that and reports: New note ID is the same, doing nothing, due to the id of current file never changes.

Even if I want to change to another name, e.g. :ObsidianRename another_new, the filename of current file would be changed to another_new.md, the id of current file still remains 20240820-old, and the references from all other files would be kept as
[[new]] due to the id does not match, which means backlink of current file could not be resolved.

Config

return {
    "epwalsh/obsidian.nvim",
    -- version = "*",
    -- lazy = true,
    ft = "markdown",
    dependencies = {
        "nvim-lua/plenary.nvim",
        "nvim-telescope/telescope.nvim",
        "hrsh7th/nvim-cmp",
    },
    keys = {
        {"<leader>fnd", "<cmd>ObsidianDailies<cr>", desc = "[Obsidian] Open daily notes"},
        {"<leader>fnt", "<cmd>ObsidianTemplate<cr>", desc = "[Obsidian] Insert template"},
        {"<leader>fnT", "<cmd>ObsidianNewFromTemplate<cr>", desc = "[Obsidian] Create note using template"},
        {"<leader>fnn", "<cmd>ObsidianQuickSwitch<cr>", desc = "[Obsidian] Open/Create new note"},
        {"<leader>fnp", "<cmd>ObsidianPasteImg<cr>", desc = "[Obsidian] Paste image into note"},
        {"<leader>fnr", "<cmd>ObsidianRename<cr>", desc = "[Obsidian] Rename note"},
        {"<leader>fnf", "<cmd>ObsidianSearch<cr>", desc = "[Obsidian] Global Search"},
        {"<leader>fnb", "<cmd>ObsidianBacklinks<cr>", desc = "[Obsidian] Search Backlink"},
        {"<leader>fno", "<cmd>ObsidianOpen<cr>", desc = "[Obsidian] Open note in Obsidian"},
    },
    opts = {
        workspaces = {
            {
                name = "Junk",
                path = "~/Obsidian/Junk/"
            },
        },
        notes_subdir = "notes",
        new_notes_location = "notes_subdir",
        log_level = vim.log.levels.INFO,
        daily_notes = {
            folder = "dailies",
            date_format = "%Y-%m-%d",
            alias_format = "%B %-d, %Y",
            default_tags = { "daily-notes" },
            -- Optional, if you want to automatically insert a template from your template directory like 'daily.md'
            template = '[Daily].md'
        },
        completion = {
            -- Set to false to disable completion.
            nvim_cmp = true,
            -- Trigger completion at 2 chars.
            min_chars = 2,
        },
        -- Optional, configure key mappings. These are the defaults. If you don't want to set any keymappings this
        -- way then set 'mappings = {}'.
        mappings = {
            -- Overrides the 'gf' mapping to work on markdown/wiki links within your vault.
            ["<leader>fnf"] = {
                action = function()
                    return require("obsidian").util.gf_passthrough()
                end,
                opts = { noremap = false, expr = true, buffer = true },
            },
            -- Toggle check-boxes.
            ["<cr>"] = {
                action = function()
                    return require("obsidian").util.toggle_checkbox()
                end,
                opts = { buffer = true },
            },
            -- Smart action depending on context, either follow link or toggle checkbox.
            ["<Tab>"] = {
                action = function()
                    return require("obsidian").util.smart_action()
                end,
                opts = { buffer = true, expr = true },
            }
        },
        templates = {
            folder = "../templates",
            date_format = "%Y-%m-%d",
            time_format = "%H:%M",
            substitutions = {},
        },
        attachments = {
            -- The default folder to place images in via `:ObsidianPasteImg`.
            -- If this is a relative path it will be interpreted as relative to the vault root.
            -- You can always override this per image by passing a full path to the command instead of just a filename.
            img_folder = "assets/imgs" .. vim.fn.expand('%:t:r'),  -- This is the default

            -- Optional, customize the default name or prefix when pasting images via `:ObsidianPasteImg`.
            ---@return string
            img_name_func = function()
              -- Prefix image names with timestamp.
                return string.format("%s-", os.date("%y%m%d%H%M%S"))
            end,

            -- A function that determines the text to insert in the note when pasting an image.
            -- It takes two arguments, the `obsidian.Client` and an `obsidian.Path` to the image file.
            -- This is the default implementation.
            ---@param client obsidian.Client
            ---@param path obsidian.Path the absolute path to the image file
            ---@return string
            img_text_func = function(client, path)
                path = client:vault_relative_path(path) or path
                return string.format("![%s](%s)", string.sub(path.name, 14), path)
            end,
        },
        -- Optional, customize how note IDs are generated given an optional title.
        ---@param title string|?
        ---@return string
        note_id_func = function(title)
          -- Create note IDs in a Zettelkasten format with a timestamp and a suffix.
          -- In this case a note with the title 'My new note' will be given an ID that looks
          -- like '1657296016-my-new-note', and therefore the file name '1657296016-my-new-note.md'
          local suffix = ""
          if title ~= nil then
            -- If title is given, transform it into valid file name.
            suffix = title:gsub(" ", "-"):gsub("[^A-Za-z0-9-]", ""):lower()
          else
            -- If title is nil, just add 4 random uppercase letters to the suffix.
            for _ = 1, 4 do
              suffix = suffix .. string.char(math.random(65, 90))
            end
          end
          return tostring(os.time()) .. "-" .. suffix
        end,

        -- Optional, customize how note file names are generated given the ID, target directory, and title.
        ---@param spec { id: string, dir: obsidian.Path, title: string|? }
        ---@return string|obsidian.Path The full path to the new note.
        note_path_func = function(spec)
          -- This is equivalent to the default behavior.
          local path = spec.dir / tostring(spec.title)
          return path:with_suffix(".md")
        end,
        }
    }

Environment

NVIM v0.10.0
Build type: Release
LuaJIT 2.1.1713484068
Run "nvim -V1 -v" for more info
Obsidian.nvim v3.9.0 (14e0427bef6c55da0d63f9a313fd9941be3a2479)
Status:
  ? buffer directory: nil
  ? working directory: C:/Users/junk
Workspaces:
  ? active workspace: Workspace(name='Junk', path='C:/Users/junk/Obsidian/Junk', root='C:/Users/junk/Obsidian/Junk')
Dependencies:
  ? plenary.nvim: a3e3bc82a3f95c5ed0d7201546d5d2c19b20d683
  ? nvim-cmp: ae644feb7b67bf1ce4260c231d1d4300b19c6f30
  ? telescope.nvim: a0bbec21143c7bc5f8bb02e0005fa0b982edc026
Integrations:
  ? picker: TelescopePicker()
  ? completion: enabled (nvim-cmp) ? refs, ? tags, ? new
    all sources:
      ? nvim_lsp
      ? luasnip
      ? buffer
      ? path
Tools:
  ? rg: ripgrep 14.1.0 (rev e50df40a19)
Environment:
  ? operating system: Windows
Config:
  ? notes_subdir: notes

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions