Skip to content

Vault

Official

Work with files and folders stored inside a vault.

Import:

import { Vault } from 'obsidian';

Signature:

export class Vault extends Events

Extends: Events

Constructor

new Vault(adapter: DataAdapter)

Constructor.

To get the constructor instance, use getVaultConstructor from obsidian-typings/implementations.

Properties

Property Type Description
_ Record<string, EventsEntry[]> Internal storage of registered event handlers by event name.
(Inherited from Events)
adapter DataAdapter The low-level adapter of the vault.
cacheLimit number Max size of the cache in bytes
config AppVaultConfig Object containing all config settings for the vault (editor, appearance, ... settings).
configDir string Gets the path to the config folder. This value is typically .obsidian but it could be different.
configTs number Timestamp of the last config change
fileMap VaultFileMapRecord Mapping of path to Obsidian folder or file structure
onChange FileSystemWatchHandler Listener for all events on the vault
requestSaveConfig Debouncer<[], Promise<void>> Debounced function for saving config.
root TFolder The same obsidian#TFolder object as .fileMap["/"]

Methods

Method Returns Description
addChild(file) void Add file as child/parent to respective folders
append(file, data, options?) Promise<void> Add text to the end of a plaintext file inside the vault.
appendBinary(normalizedPath, data, options?) Promise<void> Add data to the end of a binary file inside the vault.
cachedRead(file) Promise<string> Read the content of a plaintext file stored inside the vault. Use this if you only want to display the content to the user. If you want to modify the file content afterward use Vault.read
checkForDuplicate(file, newPath) boolean Check whether new file path is available
checkPath(path) boolean Check whether path has valid formatting (no dots/spaces at end, ...)
copy(file, newPath) Promise<T> Create a copy of a file or folder.
create(path, data, options?) Promise<TFile> Create a new plaintext file inside the vault.
createBinary(path, data, options?) Promise<TFile> Create a new binary file inside the vault.
createFolder(path) Promise<TFolder> Create a new folder inside the vault.
delete(file, force?) Promise<void> Deletes the file completely.
deleteConfigJson(configFile) Promise<void> Remove a vault config file
exists(path, isCaseSensitive?) Promise<boolean> Check whether a path exists in the vault.
generateFiles(e, t) Promise<void> Generate and register files from an async generator.
getAbstractFileByPath(path) TAbstractFile | null Get a file or folder inside the vault at the given path. To check if the return type is a file, use instanceof TFile. To check if it is a folder, use instanceof TFolder.
getAbstractFileByPathInsensitive(path) null | TAbstractFile Get an abstract file by path, insensitive to case.
getAllFolders(includeRoot?) TFolder[] Get all folders in the vault.
getAllLoadedFiles() TAbstractFile[] Get all files and folders in the vault.
getAvailablePath(path, extension) string Get path for file that does not conflict with other existing files
getAvailablePathForAttachments(filename, extension, file) Promise<string> Get path for attachment that does not conflict with other existing files
getConfig(string) unknown Get value from config by key.
getConfigFile(configFile) string Get path to config file (relative to vault root).
getDirectParent(file) null | TFolder Get direct parent of file.
getFileByPath(path) TFile | null Get a file inside the vault at the given path.
getFiles() TFile[] Get all files in the vault.
getFolderByPath(path) TFolder | null Get a folder inside the vault at the given path.
getMarkdownFiles() TFile[] Get all Markdown files in the vault.
getName() string Gets the name of the vault.
getResourcePath(file) string Returns a URI for the browser engine to use, for example to embed an image.
getRoot() TFolder Get the root folder of the current vault.
isEmpty() boolean Check whether files map cache is empty
iterateFiles(files, cachedRead) void Iterate over the files and read them
load() Promise<void> Load vault adapter
modify(file, data, options?) Promise<void> Modify the contents of a plaintext file.
modifyBinary(file, data, options?) Promise<void> Modify the contents of a binary file.
off(name, callback) void Remove an event listener.
(Inherited from Events)
offref(ref) void Remove an event listener by reference.
(Inherited from Events)
on('create', callback, ctx?) EventRef Called when a file is created. This is also called when the vault is first loaded for each existing file If you do not wish to receive create events on vault load, register your event handler inside Workspace.onLayoutReady.
on('modify', callback, ctx?) EventRef Called when a file is modified.
on('delete', callback, ctx?) EventRef Called when a file is deleted.
on('rename', callback, ctx?) EventRef Called when a file is renamed.
on('config-changed', callback, ctx?) EventRef Called whenever any of Obsidian's settings are changed.
on('raw', callback, ctx?) EventRef Triggered whenever a file gets loaded internally
on(name, callback, ctx?) EventRef Add an event listener.
(Inherited from Events)
process(file, fn, options?) Promise<string> Atomically read, modify, and save the contents of a note.
read(file) Promise<string> Read a plaintext file that is stored inside the vault, directly from disk. Use this if you intend to modify the file content afterwards. Use Vault.cachedRead otherwise for better performance.
readBinary(file) Promise<ArrayBuffer> Read the content of a binary file stored inside the vault.
readConfigJson(config) Promise<null | object> Read a config file from the vault and parse it as JSON.
readJson(path) Promise<null | object> Read a config file (full path) from the vault and parse it as JSON.
readPluginData(path) Promise<null | object> Read a plugin config file (full path relative to vault root) from the vault and parse it as JSON.
readRaw(path) Promise<string> Read a file from the vault as a string.
recurseChildren(root, cb) void
reloadConfig() void Reload all config files
removeChild(file) void Remove file as child/parent from respective folders.
rename(file, newPath) Promise<void> Rename or move a file. To ensure links are automatically renamed, use FileManager.renameFile instead.
resolveFilePath(path) null | TAbstractFile Get the file by absolute path.
resolveFileUrl(url) null | TAbstractFile Get the file by Obsidian URL
saveConfig() Promise<void> Save app and appearance configs to disk
setConfig(key, value) void Set value of config by key.
setConfigDir(configDir) void Set where the config files are stored (relative to vault root).
setFileCacheLimit(limit) void Set file cache limit
setupConfig() Promise<void> Load all config files into memory
trash(file, system) Promise<void> Tries to move to system trash. If that isn't successful/allowed, use local trash.
trigger(name, data?) void Trigger an event, executing all the listeners in order even if some of them throw an error.
(Inherited from Events)
tryTrigger(evt, args) void Try to trigger an event, executing all the listeners in order even if some of them throw an error.
(Inherited from Events)
writeConfigJson(config, data, pretty?) Promise<void> Write a config file to disk.
writeJson(path, data, pretty?) Promise<void> Write a config file (full path) to disk.
writePluginData(path, data, pretty?) Promise<void> Write a plugin config file (path relative to vault root) to disk.

Links to this page: