SM Vdirsyncer and Khard


Published on 2020-07-12 #tech

In the last post, I discussed about how to setup calendar sync using vdirsyncer and how to manage the calendar in the terminal using khal. In this I will discuss how to sync your contacts using vdirsyncer and how to manage them using khard.

As I used Vdirsyncer to sync calendars using CalDAV, in this post, I will use it sync contacts over CardDAV. As before I will show installation commands for Arch Linux.

Setup Vdirsyncer

In the previous post, I explained how to setup sync your calendar with CalDAV, in this section, I will do that same for contacts. CalDAV and CardDAV are very similar services that are based on WebDAV. To setup the contacts sync, add the following section to the vdirsyncer configuration.

[pair default_contacts]
a = "defatul_contacts_local"
b = "defatul_contacts_remote"
collections = ["from a", "from b"]
conflict_resolution = "a wins"

[storage default_contacts_local]
type = "filesystem"
path = "~/.local/share/vdirsyncer/carddav"
fileext = ".vcf"

[storage default_contacts_remote]
type = "carddav"
url = "https://contacts.example.com/carddav"
username = "user@example.com"
password.fetch = ["command", "pass", "show", "email/user@example.com"]

Similar to the calendar setup, the setup for local and remote are separated. Where the local storage is filesystem folder and the remote storage is a CardDAV server that you have access.

Once the config is setup, make vdirsyncer setup the contacts folder structure and test the remote connection.

vdirsyncer discover default_contacts

Once the setup is done, you can sync your contacts using the following command.

Vdirsyncer sync default_contacts

I use cron to regularly sync the local and remote contacts.

Install and setup Khard

Now the vCard files are synced using vdirsyncer, Khard can be used to manage your address book. Khard is a CLI application that is super simple to use and can be easily integrated into a terminal mail client (I will discuss about my email setup soon).

You can install Khard using the following command.

sudo pacman -S khard

Khard needs to be configured to use the synced address book. This is the configuration I use to access my contact list. It is very much based on the example config file in the Khard repo.

# ~/.config/khard/khard.conf

[addressbooks]
[[contacts]]
path = ~/.local/share/vdirsyncer/carddav/contacts/

[general]
debug = no
default_action = list
# These are either strings or comma seperated lists
editor = nvim, -i, NONE
merge_editor = vimdiff

[contact table]
display = first_name
group_by_addressbook = no
reverse = no
show_nicknames = no
show_uids = yes
sort = last_name
localize_dates = yes
preferred_phone_number_type = pref, cell, home
preferred_email_address_type = pref, work, home

[vcard]
private_objects = Twitter, Mastodon
preferred_version = 3.0
search_in_source_files = no
skip_unparsable = no

This configuration file is pretty much the same as the example config in the repo. I have just modified it to point to the correct address book directory and change the editor I use. Everything else remains as default.

With this now configured, I can now access my contacts with the khal list command or other commands available to the user.