Full examples of modifying part of a file with chezmoi #3996
-
Hi, I am trying to use the 'modify_' approach here https://www.chezmoi.io/user-guide/manage-different-types-of-file/ to handle just part of a file. But I am not fully understanding what files I need to to do 'chezmoi add' for, the original file (let's call it x.rc) or the modify_x.rc, or both? There is even an issue #1746 asking about an example, but it's not clear what file needs to be added either. much appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
You need to create the |
Beta Was this translation helpful? Give feedback.
-
thanks Tom, couple follow up questions...
|
Beta Was this translation helpful? Give feedback.
-
Update: In case this is useful for someone. I think I found I solution that works well for me. I wanted:
I added a hook pre re-add:
And in the sh I just use sed to remove the lines I want to discard (of course other modifications would be possible). Maybe would be worth mentioning this option here https://www.chezmoi.io/user-guide/manage-different-types-of-file/#manage-part-but-not-all-of-a-file ? |
Beta Was this translation helpful? Give feedback.
When using a
modify_
script, the contents of the file are no longer managed in the "traditional" way by chezmoi. Put simply, chezmoi just feeds the current contents of the file to the script viastdin
, and uses itsstdout
to set the new contents of the file.If you have changes you want to propagate, this would need to be done with the script itself, not the file.
You might want to explore using template functions such as
fromIni
,toIni
,setValueAtPath
,deleteValueAtPath
etc. if you require something more robust than simple pattern replacements.