Skip to content

Filesystem, Path, and Environment API

File System Operations

readFile

Read entire file contents as UTF-8 string Throws if file doesn't exist, isn't readable, or isn't valid UTF-8. For binary files, this will fail. For large files, consider memory usage.

typescript
readFile(path: string): Promise<string>

Parameters:

NameTypeDescription
pathstringFile path (absolute or relative to cwd)

writeFile

Write string content to a NEW file (fails if file exists) Creates a new file with the given content. Fails if the file already exists to prevent plugins from accidentally overwriting user data.

typescript
writeFile(path: string, content: string): Promise<void>

Parameters:

NameTypeDescription
pathstringDestination path (absolute or relative to cwd)
contentstringUTF-8 string to write

fileExists

Check if a path exists (file, directory, or symlink) Does not follow symlinks; returns true for broken symlinks. Use fileStat for more detailed information.

typescript
fileExists(path: string): boolean

Parameters:

NameTypeDescription
pathstringPath to check (absolute or relative to cwd)

fileStat

Get metadata about a file or directory Follows symlinks. Returns exists=false for non-existent paths rather than throwing. Size is in bytes; directories may report 0.

typescript
fileStat(path: string): FileStat

Parameters:

NameTypeDescription
pathstringPath to stat (absolute or relative to cwd)

readDir

List directory contents Returns unsorted entries with type info. Entry names are relative to the directory (use pathJoin to construct full paths). Throws on permission errors or if path is not a directory. const entries = editor.readDir("/home/user"); for (const e of entries) { const fullPath = editor.pathJoin("/home/user", e.name); }

typescript
readDir(path: string): DirEntry[]

Parameters:

NameTypeDescription
pathstringDirectory path (absolute or relative to cwd)

Example:

typescript
const entries = editor.readDir("/home/user");
for (const e of entries) {
const fullPath = editor.pathJoin("/home/user", e.name);
}

Environment Operations

getEnv

Get an environment variable

typescript
getEnv(name: string): string

Parameters:

NameTypeDescription
namestringName of environment variable

getCwd

Get the editor's current working directory Returns the editor's working directory (set when the editor was started). Use as base for resolving relative paths and spawning processes. Note: This returns the editor's stored working_dir, not process CWD, which is important for test isolation.

typescript
getCwd(): string

Path Operations

pathJoin

Join path segments using the OS path separator Handles empty segments and normalizes separators. If a segment is absolute, previous segments are discarded. pathJoin("/home", "user", "file.txt") // "/home/user/file.txt" pathJoin("relative", "/absolute") // "/absolute"

typescript
pathJoin(parts: string[]): string

Parameters:

NameTypeDescription
partsstring[]Path segments to join

Example:

typescript
pathJoin("/home", "user", "file.txt") // "/home/user/file.txt"
pathJoin("relative", "/absolute") // "/absolute"

pathDirname

Get the parent directory of a path Returns empty string for root paths or paths without parent. Does not resolve symlinks or check existence. pathDirname("/home/user/file.txt") // "/home/user" pathDirname("/") // ""

typescript
pathDirname(path: string): string

Parameters:

NameTypeDescription
pathstringFile or directory path

Example:

typescript
pathDirname("/home/user/file.txt") // "/home/user"
pathDirname("/") // ""

pathBasename

Get the final component of a path Returns empty string for root paths. Does not strip file extension; use pathExtname for that. pathBasename("/home/user/file.txt") // "file.txt" pathBasename("/home/user/") // "user"

typescript
pathBasename(path: string): string

Parameters:

NameTypeDescription
pathstringFile or directory path

Example:

typescript
pathBasename("/home/user/file.txt") // "file.txt"
pathBasename("/home/user/") // "user"

pathExtname

Get the file extension including the dot Returns empty string if no extension. Only returns the last extension for files like "archive.tar.gz" (returns ".gz"). pathExtname("file.txt") // ".txt" pathExtname("archive.tar.gz") // ".gz" pathExtname("Makefile") // ""

typescript
pathExtname(path: string): string

Parameters:

NameTypeDescription
pathstringFile path

Example:

typescript
pathExtname("file.txt") // ".txt"
pathExtname("archive.tar.gz") // ".gz"
pathExtname("Makefile") // ""

pathIsAbsolute

Check if a path is absolute On Unix: starts with "/". On Windows: starts with drive letter or UNC path.

typescript
pathIsAbsolute(path: string): boolean

Parameters:

NameTypeDescription
pathstringPath to check

Event/Hook Operations

Released under the Apache 2.0 License