chezmoi.nvim is a plugin designed to assist in editing and applying chezmoi-managed files within neovim. A notable distinction from the command line tool chezmoi is that chezmoi.nvim utilizes built-in neovim functions for file editing, allowing us to edit and watch multiple files simultaneously.
- Neovim (v0.9.0) or the latest version
- nvim-lua/plenary.nvim
- chezmoi latest version
- telescope.nvim (optional)
-- Lazy.nvim
{
'xvzc/chezmoi.nvim',
dependencies = { 'nvim-lua/plenary.nvim' },
config = function()
require("chezmoi").setup {
-- your configurations
}
end
},{
edit = {
watch = false,
force = false,
ignore_patterns = {
"run_onchange_.*",
"run_once_.*",
"%.chezmoiignore",
"%.chezmoitemplate",
-- Add custom patterns here
},
},
events = {
on_open = {
notification = {
enable = true,
msg = "Opened a chezmoi-managed file",
opts = {},
},
},
on_watch = {
notification = {
enable = true,
msg = "This file will be automatically applied",
opts = {},
},
},
on_apply = {
notification = {
enable = true,
msg = "Successfully applied",
opts = {},
},
},
},
telescope = {
select = { "<CR>" },
},
}The ignore_patterns option accepts Lua patterns to match against filenames. Files matching these patterns will not trigger automatic chezmoi apply when saved, even if watch mode is enabled.
The below configuration wll allow you to automatically apply changes on files under chezmoi source path.
-- e.g. ~/.local/share/chezmoi/*
vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
pattern = { os.getenv("HOME") .. "/.local/share/chezmoi/*" },
callback = function(ev)
local bufnr = ev.buf
local edit_watch = function()
require("chezmoi.commands.__edit").watch(bufnr)
end
vim.schedule(edit_watch)
end,
}){
-- ...
events = {
on_open = {
-- NOTE: This will override the default behavior of callback functions,
-- including the invocation of notifications. If you want to override
-- the default behavior but still show a notification on certain events,
-- you should define the notification logic within your override function.
override = function(bufnr)
vim.notify("Opened a chezmoi-managed file")
end,
},
-- ...
}
-- telscope-config.lua
local telescope = require("telescope")
telescope.setup {
-- ... your telescope config
}
telescope.load_extension('chezmoi')
vim.keymap.set('n', '<leader>cz', telescope.extensions.chezmoi.find_files, {})
-- You can also search a specific target directory and override arguments
-- Here is an example with the default args
vim.keymap.set('n', '<leader>fc', function()
telescope.extensions.chezmoi.find_files({
targets = vim.fn.stdpath("config"),
-- This overrides the default arguments used with 'chezmoi list'
args = {
"--path-style",
"absolute",
"--include",
"files",
"--exclude",
"externals",
}
})
end, {}):ChezmoiEdit <target> <args>
" This will open '~/.local/chezmoi/dot_zshrc' and apply the changes on save
" :ChezmoiEdit ~/.zshrc --watch
" Arguments
" --watch Automatically apply changes on save
" --force force apply.
:ChezmoiList <args>
" :ChezmoiList --include=files
" You can put any of command line arguments of 'chezmoi' hereSee Commands for more information
local managed_files = require("chezmoi.commands").list()
print(vim.inspect(managed_files))-- NOTE: chezmoi.nvim utilizes builtin neovim functions for file editing instead of `chzmoi edit`
require("chezmoi.commands").edit({
targets = { "~/.zshrc" },
args = { "--watch" }
})