2016-09-07 03:19:16 +00:00
---
title: "Alternate Files"
permalink: /docs/alternates
---
When possible, it is best to use the same files across all systems. However,
there are occasions when you need different files in some places. Below are
features and strategies for dealing with those occasions.
## Symlink alternates
It can be useful to have an automated way of choosing an alternate version of a
2019-10-20 20:07:13 +00:00
file for a different operating system, host, user, etc.
2016-09-07 03:19:16 +00:00
2019-10-20 20:07:13 +00:00
yadm will automatically create a symbolic link to the appropriate version of a
file, when a valid suffix is appended to the filename. The suffix contains the
conditions that must be met for that file to be used.
2016-09-07 03:19:16 +00:00
2019-10-20 20:07:13 +00:00
The suffix begins with `##` , followed by any number of conditions separated by
commas.
2016-09-07 03:19:16 +00:00
2019-10-20 20:07:13 +00:00
##< condition > [,< condition > ,…]
2016-09-07 03:19:16 +00:00
2019-10-20 20:07:13 +00:00
Each condition is an attribute/value pair, separated by a period. Some
conditions do not require a "value", and in that case, the period and value can
be omitted. Most attributes can be abbreviated as a single letter.
2016-09-07 03:19:16 +00:00
2019-10-20 20:07:13 +00:00
| Attribute | Meaning |
| - | - |
2020-10-18 19:40:54 +00:00
| `template` , `t` | Valid when the value matches a supported template processor. See the [Templates ](/docs/templates ) section for more details. |
2019-10-20 20:07:13 +00:00
| `user` , `u` | Valid if the value matches the current user. Current user is calculated by running < code > id ‑ u ‑ n</ code > . |
2020-06-30 21:30:31 +00:00
| `distro` , `d` | Valid if the value matches the distro. Distro is calculated by running < code > lsb_release ‑ si</ code > or inspecting < code > /etc/os-release</ code > |
2019-11-14 14:10:06 +00:00
| `os` , `o` | Valid if the value matches the OS. OS is calculated by running < code > uname ‑ s</ code > . < sup > *</ sup > |
2019-10-20 20:07:13 +00:00
| `class` , `c` | Valid if the value matches the local.class configuration. Class must be manually set using < code > yadm config local.class < class> </ code > . |
2020-10-18 19:51:34 +00:00
| `hostname` , `h` | Valid if the value matches the short hostname. Hostname is calculated by running < code > uname ‑ n</ code > , and trimming off any domain. |
2020-10-18 19:40:54 +00:00
| `default` | Valid when no other alternate is valid. |
2020-10-18 19:41:14 +00:00
| `extension` , `e` | A special "condition" that doesn't affect the selection process. Its purpose is instead to allow the alternate file to end with a certain extension to e.g. make editors highlight the content properly. |
2016-09-07 03:19:16 +00:00
2019-11-14 14:10:06 +00:00
< sub > < sup > *
The OS for "Windows Subsystem for Linux" is reported as "WSL", even though uname identifies as "Linux".
2020-06-30 21:30:31 +00:00
< br / >
*
2021-01-12 23:06:08 +00:00
The OS for Linux-like runtimes for Windows (e.g. MinGW, Cygwin) is obtained by running `uname -o` .
< br / >
*
2020-06-30 21:30:31 +00:00
If `lsb_release` is not available, "distro" will be the ID specified in `/etc/os-release` .
2019-11-14 14:10:06 +00:00
< / sup > < / sub >
2019-10-20 20:07:13 +00:00
You may use any number of conditions, in any order. An alternate will only be
used if _ALL_ conditions are valid. For all files managed by yadm's repository
or listed in `$HOME/.config/yadm/encrypt` , if they match this naming convention,
symbolic links will be created for the most appropriate version.
2016-09-07 03:19:16 +00:00
2019-10-20 20:07:13 +00:00
The "most appropriate" version is determined by calculating a score for each
version of a file. A [template ](/docs/templates ) is always scored higher than
any symlink condition. The number of conditions is the next largest factor in
scoring. Files with more conditions will always be favored. Any invalid
condition will disqualify that file completely.
2016-09-07 03:19:16 +00:00
2019-10-20 20:07:13 +00:00
If you don't care to have all versions of alternates stored in the same
directory as the generated symlink, you can place them in the
`$HOME/.config/yadm/alt` directory. The generated symlink or processed template
will be created using the same relative path.
2016-09-07 03:19:16 +00:00
2019-10-20 20:07:13 +00:00
Alternate linking may best be demonstrated by example. Assume the following
files are managed by yadm's repository:
2016-09-07 03:19:16 +00:00
2019-10-20 20:07:13 +00:00
$HOME/path/example.txt##default
$HOME/path/example.txt##class.Work
$HOME/path/example.txt##os.Darwin
$HOME/path/example.txt##os.Darwin,hostname.host1
$HOME/path/example.txt##os.Darwin,hostname.host2
$HOME/path/example.txt##os.Linux
$HOME/path/example.txt##os.Linux,hostname.host1
$HOME/path/example.txt##os.Linux,hostname.host2
2016-09-07 03:19:16 +00:00
2021-01-15 12:52:56 +00:00
If running on a MacBook named `host2` , yadm will create a symbolic link which looks like this:
2016-09-07 03:19:16 +00:00
2019-10-20 20:07:13 +00:00
`$HOME/path/example.txt` → `$HOME/path/example.txt##os.Darwin,hostname.host2`
2016-09-07 03:19:16 +00:00
2021-01-15 12:52:56 +00:00
However, on another MacBook named `host3` , yadm will create a symbolic link which looks like this:
2017-01-31 23:29:45 +00:00
2019-10-20 20:07:13 +00:00
`$HOME/path/example.txt` → `$HOME/path/example.txt##os.Darwin`
2017-01-31 23:29:45 +00:00
2019-10-20 20:07:13 +00:00
Since the hostname doesn't match any of the managed files, the more generic
version is chosen. If running on a Linux server named `host4` , the link will be:
2016-09-07 03:19:16 +00:00
2019-10-20 20:07:13 +00:00
`$HOME/path/example.txt` → `$HOME/path/example.txt##os.Linux`
2017-01-31 23:29:45 +00:00
2019-10-20 20:07:13 +00:00
If running on a Solaris server, the link will use the default version:
2017-01-31 23:29:45 +00:00
2019-10-20 20:07:13 +00:00
`$HOME/path/example.txt` → `$HOME/path/example.txt##default`
2017-01-31 23:29:45 +00:00
2019-10-20 20:07:13 +00:00
If running on a system, with class set to `Work` , the link will be:
2017-01-31 23:29:45 +00:00
2019-10-20 20:07:13 +00:00
`$HOME/path/example.txt` → `$HOME/path/example.txt##class.Work`
2017-03-31 22:20:57 +00:00
2019-10-20 20:07:13 +00:00
If no `##default` version exists and no files have valid conditions, then no
link will be created.
2017-03-31 22:20:57 +00:00
2019-10-20 20:07:13 +00:00
Links are also created for directories named this way, as long as they have at
least one yadm managed file within them.
2017-07-07 13:31:11 +00:00
2019-10-20 20:07:13 +00:00
yadm will automatically create these links by default. This can be disabled
using the `yadm.auto-alt` configuration. Even if disabled, links can be manually
created by running `yadm alt` .
2017-03-31 22:20:57 +00:00
2019-10-20 20:07:13 +00:00
## Class and Overrides
2017-03-31 22:20:57 +00:00
2019-10-20 20:07:13 +00:00
Class is a special value which is stored locally on each host (inside the local
repository). To use alternate symlinks using `##class.<CLASS>` , you must set the
value of class using the configuration `local.class` . This is set like any other
yadm configuration—with the `yadm config` command. The following sets the
`local.class` to be "Work".
2017-03-31 22:20:57 +00:00
2019-10-20 20:07:13 +00:00
yadm config local.class Work
2017-03-31 22:20:57 +00:00
2019-10-20 20:07:13 +00:00
Similarly, the values of `os` , `hostname` , and `user` can be manually overridden
using the configuration options `local.os` , `local.hostname` , and `local.user` .
2017-03-31 22:20:57 +00:00
2019-10-20 20:07:13 +00:00
## Templates
2017-03-31 22:20:57 +00:00
2019-10-20 20:07:13 +00:00
Templates are another powerful tool for creating alternate content on each host.
See the [Templates ](/docs/templates ) documentation for full details.
2017-03-31 22:20:57 +00:00
2016-09-07 03:19:16 +00:00
## Strategies for alternate files on different systems
Where possible, you should try to use the same file on every system. Here are a few examples:
### .vimrc
```vim
let OS=substitute(system('uname -s'),"\n","","")
if (OS == "Darwin")
" do something that only makes sense on a Mac
endif
```
### .tmux.conf
# use reattach-to-user-namespace as the default command on OSX
if-shell "test -f /usr/local/bin/reattach-to-user-namespace" 'set -g default-command "reattach-to-user-namespace -l bash"'
### .bash_profile
```bash
system_type=$(uname -s)
if [ "$system_type" = "Darwin" ]; then
eval $(gdircolors $HOME/.dir_colors)
else
eval $(dircolors -b $HOME/.dir_colors)
fi
```
### .gitconfig
However, sometimes the type of file you are using doesn't allow for this type of
logic. If a configuration can do an "_include_", you can include a specific
2019-10-19 18:59:03 +00:00
alternate version using yadm. Consider these three files:
2016-09-07 03:19:16 +00:00
`.gitconfig`
```ini
[log]
decorate = short
abbrevCommit = true
[include]
path = .gitconfig.local
```
2019-12-02 11:25:40 +00:00
`.gitconfig.local##os.Darwin`
2016-09-07 03:19:16 +00:00
```ini
[user]
name = Tim Byrne
email = tim@personal.email.org
```
2019-12-02 11:25:40 +00:00
`.gitconfig.local##os.Linux`
2016-09-07 03:19:16 +00:00
```ini
[user]
name = Dr. Tim Byrne
email = dr.byrne@work.email.com
```
Configuring Git this way includes `.gitconfig.local` in the standard
2019-10-19 18:59:03 +00:00
`.gitconfig` . yadm will automatically link the correct version based on the
2017-01-10 13:50:59 +00:00
operating system. The bulk of your configurations can go in a single file, and
2016-09-07 03:19:16 +00:00
you just put the exceptions in OS-specific files.