CalDAV Calendar 是一个基于 vdirsyncer + khal 的命令行日历技能,用于在 Linux 上同步和查询 CalDAV 日历,适用于 iCloud、Google、Fastmail、Nextcloud 等服务。
用途
vdirsyncer 将 CalDAV 日历同步为本地 .ics 文件,khal 读取和写入这些文件。通过两者配合,可以在本地以命令行方式查看、搜索、创建、编辑和删除日历事件,并将变更同步回远端。
主要能力
同步:使用 vdirsyncer sync 命令。查询前或修改后都应先同步。
查看事件:khal list 查看今日事件;khal list today 7d 查看未来 7 天;khal list tomorrow 查看明日;khal list 2026-01-15 2026-01-20 查看日期范围;khal list -a Work today 查看指定日历。
搜索:khal search "meeting";khal search "dentist" --format "{start-date} {title}"。
创建事件:khal new 2026-01-15 10:00 11:00 "Meeting title";khal new 2026-01-15 "All day event" 创建全天事件;khal new tomorrow 14:00 15:30 "Call" -a Work 指定日历;khal new 2026-01-15 10:00 11:00 "With notes" :: Description goes here 添加描述。创建后需执行 vdirsyncer sync 推送变更。
编辑事件:khal edit 为交互式操作,需要 TTY,自动化时可使用 tmux。命令如 khal edit "search term"、khal edit -a CalendarName "search term"、khal edit --show-past "old event"。菜单选项包括:s 编辑标题、d 编辑描述、t 编辑时间范围、l 编辑地点、D 删除事件、n 跳过(保存更改并匹配下一个)、q 退出。编辑后需执行 vdirsyncer sync。
删除事件:使用 khal edit,然后按 D 删除。
输出格式:用于脚本时,可使用 khal list --format "{start-date} {start-time}-{end-time} {title}" today 7d;khal list --format "{uid} | {title} | {calendar}" today。可用占位符包括 {title}、{description}、{start}、{end}、{start-date}、{start-time}、{end-date}、{end-time}、{location}、{calendar}、{uid}。
缓存:khal 将事件缓存于 ~/.local/share/khal/khal.db。若同步后数据看起来陈旧,可执行 rm ~/.local/share/khal/khal.db 删除缓存。
使用方式(初始配置)
- 配置 vdirsyncer(~/.config/vdirsyncer/config)。iCloud 示例:
[general] status_path = "~/.local/share/vdirsyncer/status/"
[pair icloud_calendar] a = "icloud_remote" b = "icloud_local" collections = ["from a", "from b"] conflict_resolution = "a wins"
[storage icloud_remote] type = "caldav" url = "https://caldav.icloud.com/" username = "your@icloud.com" password.fetch = ["command", "cat", "~/.config/vdirsyncer/icloud_password"]
[storage icloud_local] type = "filesystem" path = "~/.local/share/vdirsyncer/calendars/" fileext = ".ics"
服务商 URL:iCloud 为 https://caldav.icloud.com/;Google 使用 google_calendar 存储类型;Fastmail 为 https://caldav.fastmail.com/dav/calendars/user/EMAIL/;Nextcloud 为 https://YOUR.CLOUD/remote.php/dav/calendars/USERNAME/。
- 配置 khal(~/.config/khal/config):
[calendars] [[my_calendars]] path = ~/.local/share/vdirsyncer/calendars/* type = discover
[default] default_calendar = Home highlight_event_days = True
[locale] timeformat = %H:%M dateformat = %Y-%m-%d
- 发现并同步:首次执行 vdirsyncer discover,然后执行 vdirsyncer sync。
适用场景
适合在 Linux 命令行环境中管理多个 CalDAV 日历,进行批量查询、脚本化输出、快速创建与编辑事件,以及将日历数据同步到本地文件。
限制
资料明确说明该技能在 Linux 上可用。khal edit 为交互式操作,需要 TTY,自动化时需借助 tmux。查询或修改前后需要手动执行 vdirsyncer sync 才能保持同步。其他平台支持情况、收费与价格信息暂未提供。