Skip to content

xvzc/chezmoi.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

chezmoi.nvim


Edit your chezmoi-managed files and automatically apply.

chezmoi.nvim demo

What Is chezmoi.nvim?

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.

Getting Started

Requirements

Installation

-- Lazy.nvim
{
  'xvzc/chezmoi.nvim',
  dependencies = { 'nvim-lua/plenary.nvim' },
  config = function()
    require("chezmoi").setup {
      -- your configurations
    }
  end
},

Configuration

-- default values
{
  edit = {
    watch = false,
    force = false,
  },
  notification = {
    on_open = true,
    on_apply = true,
    on_watch = false,
  },
  telescope = {
    select = { "<CR>" },
  },
}

Treat all files in chezmoi source directory as chezmoi files

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,
})

Telescope Integration

-- 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, {})

fzf-lua Integration

fzf_chezmoi = function()
  require'fzf-lua'.fzf_exec(require("chezmoi.commands").list(), {
    actions = {
      ['default'] = function(selected, opts)
	require("chezmoi.commands").edit({
	  targets = {"~/" .. selected[1]},
	  args = { "--watch" }
	})
      end
    }
  })
end

vim.api.nvim_command('command! ChezmoiFzf lua fzf_chezmoi()')

User Command

: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' here

API

See Commands for more information

List

local managed_files = require("chezmoi.commands").list()
print(vim.inspect(managed_files))

Edit

-- NOTE: chezmoi.nvim utilizes builtin neovim functions for file editing instead of `chzmoi edit`
require("chezmoi.commands").edit({
    targets = { "~/.zshrc" },
    args = { "--watch" }
})