ztcl::codefile
Reads, searches, edits, and tests a source tree with short output for a coding agent.
ztcl::code SUBCOMMAND ?ARG ...?
Description
The tools of a coding agent. Each result is short: a line of a file is N<tab>TEXT, a change is FILE:A-B +ADDED -REMOVED, a long list ends with the count of the rest, and an error gives the lines for the next try. ztcl::code is the guide, with the program ztcl code that runs the tools from a shell.
Load the module with package require ztcl::code. It needs the capabilities file and channels. run, the file list of git, and the model backends claude and codex also need exec, and the backend http needs socket and the module ztcl::json.
An error of the module has a code that starts with ZTCL CODE: USAGE, NOFILE, NOTFILE, BINARY, NOMATCH, AMBIGUOUS, OVERLAP, RANGE, EXPECT, NONAME, LANG, PATTERN, CAPABILITY, and LLM.
The examples of this page run with --allow file,channels,exec,socket in a new folder with the files a.zig, b.txt, and c.md. They use no model.
Subcommands
| Subcommand | Description |
|---|---|
ztcl::code grep | Searches the files of the tree and gives the hits by file. |
ztcl::code def | Gives each definition of a name in the tree, with its line range. |
ztcl::code outline | Gives the definitions of each file with their line ranges. |
ztcl::code show | Gives lines of a file with their numbers. |
ztcl::code files | Gives the files of the tree. |
ztcl::code tree | Gives the folders of the tree with the count of files of each one. |
ztcl::code replace | Changes the one place of a text in a file. |
ztcl::code edit | Makes several changes in one file, all of them or none. |
ztcl::code lines | Replaces a range of lines of a file. |
ztcl::code insert | Puts lines before a line of a file. |
ztcl::code write | Makes or replaces a file. |
ztcl::code sub | Changes a text in each file of the tree. |
ztcl::code run | Runs a program and gives short output. |
ztcl::code digest | Gives the answer of a cheap model to a question about a text. |
ztcl::code help | Gives a list of the subcommands with their options. |
ztcl::code script | Runs a script one command at a time and prints the result of each command. |
Returns
The result of the subcommand.
Examples
Each example of this page runs after
package require ztcl::code
# A folder with three files, outside a git tree.
set dir /tmp/ztcl-ref-code-[pid]
file delete -force $dir
file mkdir $dir
cd $dir
proc put {path text} {
set ch [open $path w]
puts -nonewline $ch $text
close $ch
}
put a.zig "const std = @import(\"std\");\n\npub const Point = struct {\n x: i32,\n\n pub fn len(p: Point) i32 {\n return p.x;\n }\n};\n"
put b.txt "one\ntwo\ntwo\n"
put c.md "# Title\n\n## Part\n\ntext\n"
# The tests use no model.
set ::ztcl::code::backend {http ""}
set ::ztcl::code::llm_url http://127.0.0.1:9/v1/chat/completions
.
% ztcl::code show b.txt 2-3
2 two
3 two
Errors
% ztcl::code bogus
unknown or ambiguous subcommand "bogus": must be def, digest, edit, files, grep, help, insert, lines, outline, replace, run, script, show, sub, tree, or write
-errorcode TCL LOOKUP SUBCOMMAND bogus
% ztcl::code
wrong # args: should be "ztcl::code subcommand ?arg ...?"
-errorcode TCL WRONGARGS
Facts
- Capability
file. A new interpreter does not have it until the host turns it on.- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.2
The full page: ztcl::code.