Mitrajeet Golsangi
Mitrajeet Golsangi

Reputation: 123

How to install Undotree in NvChad?

I am learning to use NeoVim and have started with the NvChad base configuration and have setup a basic python LSP in it.

I recently saw a video where a person was using a plugin called UndoTree and wanted to install it in my setup, but I cannot seem to install it.

Here is my plugins.lua file which contains all the plugins for the lazy.nvim package manager

local plugins = {
  {
    "williamboman/mason.nvim",
    opts = {
      ensure_installed = {
        "black",
        "pyright",
        "mypy",
        "ruff",
      },
    },
  },
  {
    "neovim/nvim-lspconfig",
    config = function ()
      require "plugins.configs.lspconfig"
      require "custom.configs.lspconfig"
    end
  },
  -- Python Language Support
  {
    "jose-elias-alvarez/null-ls.nvim",
    ft = {"python"},
    opts = function ()
      return require "custom.configs.null-ls"
    end
  },
  -- Undo-Tree
    -- Added this plugin
    {
      "mbbill/undotree",
      lazy = false,
      config = function ()
        return require "custom.configs.undotree"
      end
    },
}

return plugins

This is the custom configuration file I have written for the UndoTree Plugin

local undotree = require('undotree')

undotree.setup({
  float_diff = true,  -- using float window previews diff, set this `true` will disable layout option
  layout = "left_bottom", -- "left_bottom", "left_left_bottom"
  ignore_filetype = { 'Undotree', 'UndotreeDiff', 'qf', 'TelescopePrompt', 'spectre_panel', 'tsplayground' },
  window = {
    winblend = 30,
  },
  keymaps = {
    ['j'] = "move_next",
    ['k'] = "move_prev",
    ['J'] = "move_change_next",
    ['K'] = "move_change_prev",
    ['<cr>'] = "action_enter",
    ['p'] = "enter_diffbuf",
    ['q'] = "quit",
  },
})

This is the error message I am getting after starting nvim

enter image description here

This is my :Lazy profile output

enter image description here

Upvotes: 0

Views: 2245

Answers (3)

Gaspar Vardanyan
Gaspar Vardanyan

Reputation: 1

return {
    "mbbill/undotree",
    lazy = false,
    config = function()
        vim.cmd [[
        let g:undotree_WindowLayout=2
            let g:undotree_DiffpanelHeight=8
            " let g:undotree_DiffCommand = "delta"

            if has("persistent_undo")
                let target_path = expand('~/.undodir')

                " create the directory and any parent directories
                " if the location does not exist.
                if !isdirectory(target_path)
                    call mkdir(target_path, "p", 0700)
                endif

                let &undodir=target_path
                set undofile
            endif
        ]]

        vim.keymap.set('n', '<leader>tu', vim.cmd.UndotreeToggle, {})
    end,
},

this may also be needed:

    set backup
    set undodir=~/.config/nvim/tmp/undo//
    set backupdir=~/.config/nvim/tmp/backup//
    set directory=~/.config/nvim/tmp/swap//

    if !isdirectory(expand(&undodir))
        call mkdir(expand(&undodir), "p")
    endif
    if !isdirectory(expand(&backupdir))
        call mkdir(expand(&backupdir), "p")
    endif
    if !isdirectory(expand(&directory))
        call mkdir(expand(&directory), "p")
    endif

Upvotes: 0

Pachuca
Pachuca

Reputation: 274

I was having issues with it too. I did manual mapping instead. It worked in my instance. I got it to work with this setup. I'm not sure about the rest of the configuration that you have, but at least you should be able to setup the mappings.

Here is my setup:

in ~/.config/nvim/lua/custom/plugins.lua I had something similar to yours:

  {
    "mbbill/undotree",
    lazy = false,
  },

and in ~/.config/nvim/lua/custom/mappings.lua I have:

M.undotree = {
  n = {
    ["<C-u>"] = {
      "<cmd> UndotreeToggle <CR>",
      "Toggle undotree"
    }
  }
}

require "core.utils".load_mappings("undotree")

I hope this helps.

Upvotes: 1

liam
liam

Reputation: 11

Disclaimer: I am not an expert in neovim, lua, nvchad, or vimscript.

To the best of my understanding, this is not how you should configure undotree. This plugin is entirely implemented in vimscript and therefore, you won't be able to use require to load the module, as there is no lua module for it.

Instead, you can use something like an init function to configure the plugin using vim globals. You can configure your keymaps here as well, like the below script, but it's better to just use the mappings.lua file instead like in the example nvchad setup, which would look like:

M.general = {
  n = {
    ["<leader>j"] = { "<cmd>UndotreeToggle<CR>", "Toggle Undotree" }
...
  },
}

and your plugins.lua could just look something like this:

...
  {
    "mbbill/undotree",
    cmd = "UndotreeToggle",
    init = function()
      require "custom.inits.undotree"
    end,
  },
...

where the init file could follow a pattern like this:

vim.g.undotree_WindowLayout = 2
...

I couldn't find most of the configuration you're setting in the docs so I only included the one setting I saw existed as an example. Check out the section on undotree_WindowLayout to understand the why it's set to 2. Also, you can load the docs for this plugin by calling :help undo-tree in neovim (this applies to pretty much every plugin, also you should have tab-complete in that menu so you can explore the options for a prefix, etc).

Also note that I configured undotree to load when you call UndotreeToggle, which means you have to call that command first. This is how I would use the plugin (toggle the tree to start using undotree by using the <leader>j hotkey--or whatever else you choose to set this to), but you could change this behavior or disable lazy loading like you have already if you'd prefer that.

If anyone else knows better or if this is the wrong approach, please let me know. But I hope this helps!

Upvotes: 1

Related Questions