├── README.md └── plugin └── vim-chezmoi.vim /README.md: -------------------------------------------------------------------------------- 1 | # vim-chezmoi 2 | vim-chezmoi is a plugin for vim to apply the dotfile you are editing to [chezmoi](https://github.com/twpayne/chezmoi), on `:w`. 3 | 4 | It does not currently work with opening the source files directly(i.e `vim ~/.local/share/dotfile`), only `chezmoi edit dotfile`. 5 | 6 | # Install with vim-plug 7 | 8 | ```vim 9 | " ~/.vimrc 10 | Plug 'Lilja/vim-chezmoi' 11 | ``` 12 | 13 | and then 14 | ```vim 15 | " ~/.vimrc 16 | let g:chezmoi = "enabled" 17 | ``` 18 | 19 | ## Usage 20 | First install it using your favorite plugin manager. Then, to edit a dotfile, run `chezmoi edit `, where `` is a dotfile, like `~/.bashrc`. Now, all writes being done to that file are applied(`chezmoi apply`) automatically. 21 | 22 | ## Arguments 23 | `vim-chezmoi` has two options. You will very likely not need it. 24 | 25 | 26 | * `g:chezmoi_executable` the binary for chezmoi, a string. Default is `"chezmoi"` 27 | * `g:chezmoi_debug_mode` a debug mode for the plugin. Default value is off. 28 | -------------------------------------------------------------------------------- /plugin/vim-chezmoi.vim: -------------------------------------------------------------------------------- 1 | function s:Log(...) 2 | if g:vimChezmoiDebugMode == 1 3 | echo a:1 4 | endif 5 | endfunction 6 | 7 | function s:GetDotfileFromCommand(cmd) 8 | return a:cmd[-1] 9 | endfunction 10 | 11 | function s:GetParentPid(pid) 12 | return substitute(system("ps -o ppid= -p " . a:pid), '\n', '\1', '') 13 | endfunction 14 | 15 | function s:GetCmdline(pid) 16 | return split(system("ps -p " . a:pid . " -o args"), "\n")[-1] 17 | endfunction 18 | 19 | function g:Chezmoi(...) 20 | let l:chezmoiBinary = get(g:, "chezmoi_executable", "chezmoi") 21 | let g:vimChezmoiDebugMode = get(g:, "chezmoi_debug_mode", 0) 22 | 23 | if executable(l:chezmoiBinary) == 0 24 | echo "vim-chezmoi: The binary \"". l:chezmoiBinary . "\" does not exist. Check your vimrc and/or chezmoi installation." 25 | return 26 | endif 27 | call s:Log("Executable exists") 28 | 29 | let l:pid = getpid() 30 | call s:Log("Getting parent pid") 31 | let parentPid = s:GetParentPid(getpid()) 32 | call s:Log("Getting parent pid cmd args") 33 | let parentPidFileContents = s:GetCmdline(parentPid) 34 | call s:Log("Parent args: " . parentPidFileContents) 35 | let l:parentPidFileSplitted = split(l:parentPidFileContents, " ") 36 | 37 | call s:Log(l:parentPidFileSplitted) 38 | if stridx(l:parentPidFileSplitted[0], l:chezmoiBinary) != -1 && l:parentPidFileSplitted[1] == "edit" 39 | let l:dotfile = s:GetDotfileFromCommand(l:parentPidFileSplitted) 40 | call s:Log("This vim session was launched via chezmoi edit. The dotfile that is being edited is " . l:dotfile) 41 | let g:chezmoiCommandToExecute = "!" . l:chezmoiBinary . " apply " . l:dotfile 42 | call s:Log("Cmd to execute on write: " . g:chezmoiCommandToExecute) 43 | 44 | if g:vimChezmoiDebugMode != 1 45 | let g:chezmoiCommandToExecute = ":silent " . g:chezmoiCommandToExecute 46 | endif 47 | au BufWritePost * execute g:chezmoiCommandToExecute 48 | 49 | else 50 | call s:Log("Chezmoi edit command not found on parent process. It's probably not a chezmoi invoke.") 51 | endif 52 | endfunction 53 | 54 | if exists("g:chezmoi") 55 | call g:Chezmoi() 56 | endif 57 | --------------------------------------------------------------------------------