Skip to content

Commit ac2a968

Browse files
Foam CLI (#1636)
Introduces the Foam CLI (`foam-cli` package) — a standalone command-line interface for working with Foam workspaces outside of VS Code. Commands added: `foam note` — create, view, and manage notes `foam rename` — rename a note and update all wikilinks pointing to it `foam list` — list notes with filtering options `foam search` — full-text search across the workspace `foam links` — inspect inbound/outbound links for a note `foam grep` — regex/glob search across notes `foam outline` — display the heading outline of a note `foam lint` — run janitor checks (missing headings, stale definitions) and optionally fix them `foam tag` — list and filter notes by tag `foam daily` — create or open daily notes Also includes full documentation under docs/user/tools/cli/ and test coverage for all commands
1 parent 40ac829 commit ac2a968

63 files changed

Lines changed: 5916 additions & 144 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/user/features/daily-notes.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,7 @@ To customize your daily note location and format you can create a `.foam/templat
6060

6161
There are also some settings to customize the behavior of daily notes, but they are deprecated and will be removed. Please use the `daily-note.md` template.
6262

63-
[templates]: templates.md "Note Templates"
63+
To work with daily notes from the terminal, see [[daily|CLI daily command]].
64+
65+
[templates]: templates.md 'Note Templates'
66+
[daily]: ../tools/cli/daily.md 'foam daily'

docs/user/features/tags.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,7 @@ Customize tag appearance in markdown preview by adding CSS:
8787

8888
Some users prefer backlinks (e.g. linking to a `book` note) instead of #book tags for categorization. Both approaches work — choose what fits your workflow.
8989

90+
To manage tags from the terminal, see [[tag|CLI tag command]].
91+
9092
[graph-view]: graph-view.md 'Graph Visualization'
93+
[tag]: ../tools/cli/tag.md 'foam tag'

docs/user/getting-started/installation.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ Other extensions:
5050
- **Paste Image** - Easily insert images from clipboard
5151
- **Todo Tree** - Track TODO items across your workspace
5252

53+
## Optional: Install the Foam CLI
54+
55+
The Foam CLI lets you work with your workspace from the terminal — search notes, manage tags, create daily notes, and more — without opening VS Code.
56+
57+
```bash
58+
npm install -g @foam/cli
59+
```
60+
61+
Once installed, run `foam --help` to see available commands, or see the [[cli|CLI documentation]] for details.
62+
5363
## What's Next?
5464

5565
Now that Foam is installed, you're ready to:
@@ -75,3 +85,4 @@ If you encounter issues:
7585
[navigation]: navigation.md 'Navigation in Foam'
7686
[graph-view]: ../features/graph-view.md 'Graph Visualization'
7787
[frequently-asked-questions]: ../frequently-asked-questions.md 'Frequently Asked Questions'
88+
[cli]: ../tools/cli.md 'Foam CLI'

docs/user/tools/cli.md

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,49 @@
1-
# Command Line Interface
1+
# Foam CLI
22

3-
Create a CLI tool to allow running common Foam commands. These may include:
3+
The `foam` command line tool lets you work with your Foam workspace from the terminal — search, inspect, and manage notes without opening VS Code.
44

5-
- `foam init` - create a new foam workspace
6-
- `foam janitor` - run [[workspace-janitor]] in current workspace
7-
- `foam migrate <tool>` - migrate from tools like roam exports, obsidian and more
5+
## Installation
86

9-
More commands to be added.
7+
```bash
8+
npm install -g @foam/cli
9+
```
1010

11-
[workspace-janitor]: workspace-janitor.md 'Janitor'
11+
## Usage
12+
13+
```
14+
foam <command> [options]
15+
```
16+
17+
Set the `FOAM_WORKSPACE` environment variable to avoid typing `--workspace` on every command:
18+
19+
```bash
20+
export FOAM_WORKSPACE=/path/to/your/notes
21+
```
22+
23+
All commands accept `--format json` for machine-readable output, useful for scripting.
24+
25+
## Commands
26+
27+
| Command | Description |
28+
| ----------- | ------------------------------------------------- |
29+
| [[daily]] | Show or create the daily note for a date |
30+
| [[grep]] | Search note content by pattern |
31+
| [[links]] | Show links to and from a note |
32+
| [[lint]] | Check notes for issues |
33+
| [[list]] | List notes, tags, orphans, placeholders, and more |
34+
| [[note]] | Show, create, move, or delete notes |
35+
| [[outline]] | Show the heading structure of a note |
36+
| [[rename]] | Rename notes, tags, sections, or block anchors |
37+
| [[search]] | Search notes by title, tag, or frontmatter |
38+
| [[tag]] | List, rename, or search tags |
39+
40+
[daily]: cli/daily.md 'foam daily'
41+
[grep]: cli/grep.md 'foam grep'
42+
[links]: cli/links.md 'foam links'
43+
[lint]: cli/lint.md 'foam lint'
44+
[list]: cli/list.md 'foam list'
45+
[note]: cli/note.md 'foam note'
46+
[outline]: cli/outline.md 'foam outline'
47+
[rename]: cli/rename.md 'foam rename'
48+
[search]: cli/search.md 'foam search'
49+
[tag]: cli/tag.md 'foam tag'

docs/user/tools/cli/daily.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# foam daily
2+
3+
Show or create the daily note for a date.
4+
5+
```
6+
foam daily [options]
7+
```
8+
9+
Without `--create`, the command only reports whether the note exists. With `--create`, it writes the note if it doesn't exist yet, using your daily note template if one is configured.
10+
11+
## Options
12+
13+
| Option | Description |
14+
| --------------------- | -------------------------------------------------------------------------- |
15+
| `--date <YYYY-MM-DD>` | Date to target (default: today) |
16+
| `--create` | Create the note if it doesn't exist |
17+
| `--path-only` | Print only the resolved file path (useful for scripting) |
18+
| `--workspace <dir>` | Workspace root (default: `FOAM_WORKSPACE` env var, then current directory) |
19+
| `--format <fmt>` | Output format: `text` (default) or `json` |
20+
21+
## Examples
22+
23+
Show today's daily note status:
24+
25+
```bash
26+
foam daily
27+
# journals/2024-01-15.md [exists]
28+
```
29+
30+
Create today's daily note:
31+
32+
```bash
33+
foam daily --create
34+
```
35+
36+
Get the path to a specific date's note (for scripting):
37+
38+
```bash
39+
foam daily --date 2024-01-10 --path-only
40+
# /home/user/notes/journals/2024-01-10.md
41+
```
42+
43+
Open today's note in your editor:
44+
45+
```bash
46+
$EDITOR $(foam daily --create --path-only)
47+
```
48+
49+
## Daily note path
50+
51+
If no daily note template is configured, notes are created at `journals/YYYY-MM-DD.md` relative to the workspace root. If a template exists (`.foam/templates/daily-note.md` or `.foam/templates/daily-note.js`), the template determines the file path and content.
52+
53+
See [[templates]] for how to configure daily note templates.
54+
55+
[templates]: ../../features/templates.md 'Note Templates'

docs/user/tools/cli/grep.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# foam grep
2+
3+
Search note content by pattern.
4+
5+
```
6+
foam grep <pattern> [options]
7+
```
8+
9+
Searches the text content of all notes in the workspace. The pattern is matched case-insensitively as a regular expression. Unlike [[search|foam search]], this command searches the full text of each note rather than indexed metadata.
10+
11+
## Options
12+
13+
| Option | Description |
14+
| ------------------- | -------------------------------------------------------------------------- |
15+
| `--context <n>` | Show `n` lines of surrounding context around each match |
16+
| `--no-line-number` | Omit line numbers from output |
17+
| `--limit <n>` | Max number of matching files to return (default: 20) |
18+
| `--workspace <dir>` | Workspace root (default: `FOAM_WORKSPACE` env var, then current directory) |
19+
| `--format <fmt>` | Output format: `text` (default) or `json` |
20+
21+
## Examples
22+
23+
Search for a word across all notes:
24+
25+
```bash
26+
foam grep "quantum"
27+
# physics/quantum-mechanics.md:3: Quantum mechanics describes...
28+
# research/reading-list.md:12: See also quantum entanglement
29+
```
30+
31+
Search with context lines:
32+
33+
```bash
34+
foam grep "TODO" --context 2
35+
```
36+
37+
Limit results:
38+
39+
```bash
40+
foam grep "meeting" --limit 5
41+
```
42+
43+
Use in a script (JSON output):
44+
45+
```bash
46+
foam grep "action item" --format json
47+
```
48+
49+
[search]: search.md 'foam search'

docs/user/tools/cli/links.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# foam links
2+
3+
Show links to and from a note.
4+
5+
```
6+
foam links (<identifier> | --path <path>) [options]
7+
```
8+
9+
Displays the outgoing links (notes this note links to) and incoming links (notes that link to this note, also called backlinks). The command can also be invoked as `foam connections`.
10+
11+
## Options
12+
13+
| Option | Description |
14+
| ------------------- | -------------------------------------------------------------------------- |
15+
| `--path <path>` | Target note by exact file path instead of identifier |
16+
| `--outgoing` | Show only outgoing links |
17+
| `--incoming` | Show only incoming links (backlinks) |
18+
| `--workspace <dir>` | Workspace root (default: `FOAM_WORKSPACE` env var, then current directory) |
19+
| `--format <fmt>` | Output format: `text` (default) or `json` |
20+
21+
By default, both outgoing and incoming links are shown.
22+
23+
## Examples
24+
25+
Show all links for a note:
26+
27+
```bash
28+
foam links my-note
29+
# Outgoing (2):
30+
# → related-topic notes/related-topic.md
31+
# → another-note notes/another-note.md
32+
#
33+
# Incoming (1):
34+
# ← index index.md
35+
```
36+
37+
Show only backlinks:
38+
39+
```bash
40+
foam links my-note --incoming
41+
```
42+
43+
Target by file path:
44+
45+
```bash
46+
foam links --path notes/my-note.md
47+
```
48+
49+
Get links as JSON for scripting:
50+
51+
```bash
52+
foam links my-note --format json
53+
```
54+
55+
## Note identifiers
56+
57+
Foam identifies notes by the shortest unique part of their filename (without extension). For example, a note at `projects/alpha/notes.md` might be identified as `notes` if that's unique, or `alpha/notes` if there's ambiguity. Use [[note|foam note id]] to check how a note is identified.
58+
59+
See also [[backlinking]] for the backlinks panel in VS Code.
60+
61+
[backlinking]: ../../features/backlinking.md 'Backlinks'
62+
[note]: note.md 'foam note'

docs/user/tools/cli/lint.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# foam lint
2+
3+
Check notes for issues.
4+
5+
```
6+
foam lint [options]
7+
```
8+
9+
Scans all notes in the workspace and reports problems such as missing headings or stale link reference definitions. With `--fix`, auto-fixable issues are corrected in place.
10+
11+
Exits with code `2` when issues are found (and no `--fix`), making it easy to use in CI pipelines.
12+
13+
## Options
14+
15+
| Option | Description |
16+
| ------------------- | -------------------------------------------------------------------------- |
17+
| `--fix` | Apply all auto-fixable issues |
18+
| `--rule <id>` | Run only the given rule (can be repeated) |
19+
| `--workspace <dir>` | Workspace root (default: `FOAM_WORKSPACE` env var, then current directory) |
20+
| `--format <fmt>` | Output format: `text` (default) or `json` |
21+
22+
## Rules
23+
24+
| Rule ID | Description | Auto-fixable |
25+
| ------------------- | ------------------------------------------------------ | :----------: |
26+
| `missing-heading` | Notes without a top-level heading | Yes |
27+
| `stale-definitions` | Link reference definitions that are outdated or unused | Yes |
28+
29+
## Exit codes
30+
31+
| Code | Meaning |
32+
| ---- | --------------- |
33+
| `0` | No issues found |
34+
| `1` | Command error |
35+
| `2` | Issues found |
36+
37+
## Examples
38+
39+
Check the workspace for issues:
40+
41+
```bash
42+
foam lint
43+
# notes/my-note.md
44+
# 5:1 warning Missing heading missing-heading
45+
#
46+
# 1 problem (0 errors, 1 warning, 1 fixable with --fix)
47+
```
48+
49+
Fix all auto-fixable issues:
50+
51+
```bash
52+
foam lint --fix
53+
```
54+
55+
Run only one rule:
56+
57+
```bash
58+
foam lint --rule missing-heading
59+
```
60+
61+
Use in CI:
62+
63+
```bash
64+
foam lint || echo "Lint issues found — please fix before merging"
65+
```
66+
67+
Get results as JSON:
68+
69+
```bash
70+
foam lint --format json
71+
```
72+
73+
See also [[workspace-janitor]] for running the janitor from VS Code.
74+
75+
[workspace-janitor]: ../workspace-janitor.md 'Workspace Janitor'

0 commit comments

Comments
 (0)