Skip to main content

A small CLI for reading and writing notes in a local Obsidian vault.

Project description

odcli

一个基于 Python 的本地 CLI,用来对 Obsidian vault 做读写操作。它直接操作 vault 里的 Markdown 文件,不依赖 Obsidian 的私有接口,因此稳定、可移植,也方便后续扩展。

功能

  • 校验 vault 路径是否有效
  • 列出 vault 中的 Markdown 笔记
  • 读取指定笔记
  • 按指定行区间读取笔记
  • 覆盖写入或自动创建笔记
  • 按指定行区间覆盖写入
  • 追加内容到笔记末尾
  • 在 vault 中全文搜索

使用 uv

cd /Users/huchang/agents/obsidian_cli
uv sync
uv run odcli --help

运行测试:

cd /Users/huchang/agents/obsidian_cli
uv run python -m unittest discover -s tests

构建分发包:

cd /Users/huchang/agents/obsidian_cli
uv build

发布到 PyPI 后,安装包名会是 odcli。 安装后可执行命令同时支持 odcliobsidian-cli

直接运行

cd /Users/huchang/agents/obsidian_cli
./odcli --help

兼容入口仍然保留:

cd /Users/huchang/agents/obsidian_cli
./obsidian-cli --help

如果你更喜欢显式调用 Python:

PYTHONPATH=src python3 -m obsidian_cli --help

可选安装

如果你想安装到虚拟环境中:

cd /Users/huchang/agents/obsidian_cli
uv sync

指定 vault

优先级如下:

  1. --vault /path/to/vault
  2. 环境变量 OBSIDIAN_VAULT
  3. Obsidian 本地配置里最近打开的 vault
  4. 常见默认目录

当前内置的默认目录包括:

  • macOS: ~/Documents/Obsidian Vault
  • macOS: ~/Documents/Obsidian
  • macOS iCloud: ~/Library/Mobile Documents/iCloud~md~obsidian/Documents
  • Windows: %USERPROFILE%\\Documents\\Obsidian Vault
  • Windows: %USERPROFILE%\\Documents\\Obsidian

示例:

export OBSIDIAN_VAULT="/Users/your-name/Documents/MyVault"
./odcli check
./odcli list
./odcli read Inbox/today.md
./odcli read-lines Inbox/today.md 3 8
./odcli write Inbox/today.md --content "# Today"
./odcli write-lines Inbox/today.md 3 4 --content "- replaced\n- lines\n"
./odcli append Inbox/today.md --content "\n- new item"
./odcli search "project alpha"

命令

check

检查 vault 是否存在,以及是否包含 .obsidian 目录。

list

列出 vault 中的 Markdown 文件。

可选参数:

  • --limit N

read

读取笔记内容。

参数:

  • note_path:相对于 vault 根目录的路径

write

覆盖写入笔记;若父目录不存在会自动创建。

参数:

  • note_path
  • --content TEXT
  • --stdin:从标准输入读取内容

可选参数:

  • --create-only:若文件已存在则报错

read-lines

读取指定行区间,行号从 1 开始,区间是闭区间。

参数:

  • note_path
  • start_line
  • end_line

write-lines

用新内容替换指定行区间,行号从 1 开始,区间是闭区间。

参数:

  • note_path
  • start_line
  • end_line
  • --content TEXT
  • --stdin

append

追加内容到笔记末尾。

参数:

  • note_path
  • --content TEXT
  • --stdin

search

在所有 Markdown 笔记中搜索文本。

参数:

  • query
  • --case-sensitive

测试

cd /Users/huchang/agents/obsidian_cli
uv run python -m unittest discover -s tests

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

odcli-0.1.2.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

odcli-0.1.2-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file odcli-0.1.2.tar.gz.

File metadata

  • Download URL: odcli-0.1.2.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for odcli-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d7c31e586a7cde9a34a8d9197cdbf4ab1bdfd98761d118829978cc279db5dfbf
MD5 e7c1775f164403666f5960d314d267c4
BLAKE2b-256 6cd2dfed535ed08b3c79cace77f8a598dc8945c4a9f6e1bdf17b9ed9fe8c2c19

See more details on using hashes here.

File details

Details for the file odcli-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: odcli-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for odcli-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e84a81d20aca8563c82f0d1ad087d88a16337dafe4ef543c0c57570e002adf33
MD5 27e90a1efc1c371d01eb4e2e205c334c
BLAKE2b-256 34c81621031935bbcbb3cd6a6473ce67d9a2d681eb567434f4b389cbd1d1724e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page