chezmoi/Q/How can I get my editors to do syntax highlighting on the dotfiles? Is there an accepted way that chezmoi allows this?
When using chezmoi, how can I get my editors to do syntax highlighting on the dotfiles? Is there an accepted way that chezmoi allows this? card
- My Answer
- Run chezmoi/edit. As long as EnvVar/EDITOR or EnvVar/VISUAL is set, it will open with syntax highlighting using a Hard Link.
- For vim, you can prepend your docs with a vim/Mode/Line that tells vim what filetype the file should be, for example,
# vim: filetype=zsh in dot_zshrc
- AI/Response
- Preferred chezmoi workflow — edit the target, not the source name: run
chezmoi edit with a destination path (for example chezmoi edit ~/.zshrc). chezmoi opens your $EDITOR on the file in its target location (by default via a hardlink when possible), so the buffer path looks like .zshrc or .config/foo/bar and normal editor filetype detection applies. The design FAQ states explicitly that chezmoi edit arguments are target paths so you do not have to reason about dot_ prefixes while editing and syntax highlighting works. - Why source-tree names are awkward: in the chezmoi source state, paths use filename metadata (
dot_bashrc, private_config, executable_, templates as *.tmpl, and so on). Those names are not the final dotfile paths, so an editor opening dot_zshrc in the repo often picks the wrong language. That is expected; chezmoi’s accepted mitigation is chezmoi edit, not a special highlighter built into chezmoi itself. - Editing directly in the source directory: chezmoi does not ship editor integrations for every editor. For Vim/Neovim, the Editor user guide documents alker0/chezmoi.vim, which maps
dot_* names to real paths for filetype detection and, for *.tmpl, layers go-template highlighting on top of the underlying language. The same guide lists Lilja/vim-chezmoi (apply on save with chezmoi edit) and xvzc/chezmoi.nvim. For Emacs, chezmoi.el is the documented option. - Vim modeline in the source file: if you insist on editing
dot_* files in the repo without a plugin, the design FAQ also recommends a Vim modeline at the top of the source file (for example # vim: filetype=zsh in dot_zshrc). That is a Vim convention chezmoi documents, not a chezmoi parser feature. - Templates: plain
chezmoi edit gives correct highlighting for non-template targets. For *.tmpl in the source tree, use chezmoi.vim (or similar) so you get both the base language and template syntax; some dotfiles repos also add modelines such as vim:ft=toml.gotexttmpl in template headers as a local convention. - VS Code / GUI editors: configure
edit.command / edit.args in ~/.config/chezmoi/chezmoi.toml (for Code, include --wait so chezmoi does not return early — see the Editor guide). Use chezmoi edit for highlighting via real target paths; for raw source-tree editing in VS Code you rely on language-specific extensions or files.associations, not a first-party chezmoi VS Code extension in the official docs. - Bottom line: chezmoi’s accepted, documented approach is
chezmoi edit → editor sees destination filenames; for source-directory and template editing, use editor plugins or modelines listed in the Editor guide and design FAQ, especially chezmoi.vim on Vim/Neovim.