Debian Codegraph @

docs/howto/merge-files-when-installing.md

raw
1 # Merge files on install
2
3 Dotdrop allows to merge multiple files into one using Jinja2's `include` directive.
4
5 For example, let's assume you want to keep your `.vimrc` split into multiple parts in dotdrop:
6 * `<dotpath>/vimrc.d/top`: top part of the file
7 * `<dotpath>/vimrc.d/bottom`: bottom part of the file
8
9 And you want dotdrop to merge all those files into `~/.vimrc` whenever you process your .vimrc with dotdrop.
10
11 First make sure `~/.vimrc` is present in your config file:
12 ```yaml
13 ...
14 dotfiles:
15 f_vimrc:
16 dst: ~/.vimrc
17 src: vimrc
18 profiles:
19 hostname:
20 dotfiles:
21 - f_vimrc
22 ...
23 ```
24
25 Note that the subfiles (`vimrc.d/top` and `vimrc.d/bottom`)
26 are not known to the config and do not need to be.
27
28 Edit the stored vimrc file to include the other files, for example:
29 ```bash
30 $ cat <dotpath>/vimrc
31 {%@@ include 'vimrc.d/top' @@%}
32 filetype on
33 set t_Co=256
34 set tw=0
35 set tabstop=2
36 set shiftwidth=2
37 set expandtab
38 set nocompatible
39 set nomodeline
40 syntax on
41 {%@@ include 'vimrc.d/bottom' @@%}
42 ```
43
44 The `include` path parameter needs to be relative to your `dotpath`.
45
46 Dotdrop will then automagically include the files into your vimrc when handling `f_vimrc`.
47
48 ## Merge all files in a directory
49
50 To include all files in a directory, a combination of
51 [dynvariables](../config/config-dynvars.md)
52 and [Jinja2 directives](https://jinja.palletsprojects.com/en/2.11.x/) have to be used.
53
54 Let's say all files in `<dotpath>/toinclude` need to be included into a dotfile.
55
56 First define a [dynvariables](../config/config-dynvars.md)
57 in the config file which will look for files to include in the above directory:
58 ```yaml
59 dynvariables:
60 allfiles: "cd {{@@ _dotdrop_dotpath @@}}; find toinclude/ -type f | xargs"
61 ```
62
63 Note that `_dotdrop_dotpath` is part of the built-in variables
64 (For more, see [template variables](../template/template-variables.md#template-variables)).
65
66 Then use the generated list in the dotfile template:
67 ```
68 {%@@ for f in allfiles.split() @@%}
69 {%@@ include f @@%}
70 {%@@ endfor @@%}
71 ```