Skip to content

Vault

Work with files and folders stored inside a vault.

See

https://docs.obsidian.md/Plugins/Vault

Extends

Constructors

new Vault()

new Vault(): Vault

Returns

Vault

Inherited from

Events.constructor

Properties

_

_: Record<string, EventsEntry[]>

Inherited from

Events._


adapter

adapter: DataAdapter


config

config: AppVaultConfig

Object containing all config settings for the vault (editor, appearance, … settings)


configDir

configDir: string

Gets the path to the config folder. This value is typically .obsidian but it could be different.

Methods

append()

append(file, data, options?): Promise<void>

Add text to the end of a plaintext file inside the vault.

Parameters

file: TFile

The file

data: string

the text to add

options?: DataWriteOptions

(Optional)

Returns

Promise<void>


cachedRead()

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

Parameters

file: TFile

Returns

Promise<string>


copy()

copy(file, newPath): Promise<TFile>

Create a copy of the selected file.

Parameters

file: TFile

The file

newPath: string

Vault absolute path for the new copy.

Returns

Promise<TFile>


create()

create(path, data, options?): Promise<TFile>

Create a new plaintext file inside the vault.

Parameters

path: string

Vault absolute path for the new file, with extension.

data: string

text content for the new file.

options?: DataWriteOptions

(Optional)

Returns

Promise<TFile>


createBinary()

createBinary(path, data, options?): Promise<TFile>

Create a new binary file inside the vault.

Parameters

path: string

Vault absolute path for the new file, with extension.

data: ArrayBuffer

content for the new file.

options?: DataWriteOptions

(Optional)

Returns

Promise<TFile>

Throws

Error if file already exists


createFolder()

createFolder(path): Promise<TFolder>

Create a new folder inside the vault.

Parameters

path: string

Vault absolute path for the new folder.

Returns

Promise<TFolder>

Throws

Error if folder already exists


delete()

delete(file, force?): Promise<void>

Deletes the file completely.

Parameters

file: TAbstractFile

The file or folder to be deleted

force?: boolean

Should attempt to delete folder even if it has hidden children

Returns

Promise<void>


exists()

exists(path, isCaseSensitive?): Promise<boolean>

Check whether a path exists in the vault

Parameters

path: string

isCaseSensitive?: boolean

Returns

Promise<boolean>


getAbstractFileByPath()

getAbstractFileByPath(path): null | TAbstractFile

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.

Parameters

path: string

vault absolute path to the folder or file, with extension, case sensitive.

Returns

null | TAbstractFile

the abstract file, if it’s found.


getAbstractFileByPathInsensitive()

getAbstractFileByPathInsensitive(path): null | TAbstractFile

Get an abstract file by path, insensitive to case

Parameters

path: string

Returns

null | TAbstractFile


getAllFolders()

getAllFolders(includeRoot?): TFolder[]

Get all folders in the vault.

Parameters

includeRoot?: boolean

Should the root folder (/) be returned

Returns

TFolder[]


getAllLoadedFiles()

getAllLoadedFiles(): TAbstractFile[]

Get all files and folders in the vault.

Returns

TAbstractFile[]


getConfig()

getConfig(string): unknown

Get value from config by key

Parameters

string: ConfigItem

Key of config value

Returns

unknown


getConfigFile()

getConfigFile(configFile): string

Get path to config file (relative to vault root)

Parameters

configFile: string

Returns

string


getDirectParent()

getDirectParent(file): null | TFolder

Get direct parent of file

Parameters

file: TAbstractFile

File to get parent of

Returns

null | TFolder


getFileByPath()

getFileByPath(path): null | TFile

Get a file inside the vault at the given path. Returns null if the file does not exist.

Parameters

path: string

Returns

null | TFile


getFiles()

getFiles(): TFile[]

Get all files in the vault.

Returns

TFile[]


getFolderByPath()

getFolderByPath(path): null | TFolder

Get a folder inside the vault at the given path. Returns null if the folder does not exist.

Parameters

path: string

Returns

null | TFolder


getMarkdownFiles()

getMarkdownFiles(): TFile[]

Get all Markdown files in the vault.

Returns

TFile[]


getName()

getName(): string

Gets the name of the vault.

Returns

string


getResourcePath()

getResourcePath(file): string

Returns an URI for the browser engine to use, for example to embed an image.

Parameters

file: TFile

Returns

string


getRoot()

getRoot(): TFolder

Get the root folder of the current vault.

Returns

TFolder


modify()

modify(file, data, options?): Promise<void>

Modify the contents of a plaintext file.

Parameters

file: TFile

The file

data: string

The new file content

options?: DataWriteOptions

(Optional)

Returns

Promise<void>


modifyBinary()

modifyBinary(file, data, options?): Promise<void>

Modify the contents of a binary file.

Parameters

file: TFile

The file

data: ArrayBuffer

The new file content

options?: DataWriteOptions

(Optional)

Returns

Promise<void>


off()

off(name, callback): void

Parameters

name: string

callback

Returns

void

Inherited from

Events.off


offref()

offref(ref): void

Parameters

ref: EventRef

Returns

void

Inherited from

Events.offref


on()

on(name, callback, ctx)

on(name, 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.

Parameters

name: "create"

callback

ctx?: any

Returns

EventRef

Inherited from

Events.on

on(name, callback, ctx)

on(name, callback, ctx?): EventRef

Called when a file is modified.

Parameters

name: "modify"

callback

ctx?: any

Returns

EventRef

Inherited from

Events.on

on(name, callback, ctx)

on(name, callback, ctx?): EventRef

Called when a file is deleted.

Parameters

name: "delete"

callback

ctx?: any

Returns

EventRef

Inherited from

Events.on

on(name, callback, ctx)

on(name, callback, ctx?): EventRef

Called when a file is renamed.

Parameters

name: "rename"

callback

ctx?: any

Returns

EventRef

Inherited from

Events.on

on(name, callback, ctx)

on(name, callback, ctx?): EventRef

Called whenever any of Obsidian’s settings are changed

Parameters

name: "config-changed"

callback

ctx?: unknown

Returns

EventRef

Inherited from

Events.on


process()

process(file, fn, options?): Promise<string>

Atomically read, modify, and save the contents of a note.

Parameters

file: TFile

the file to be read and modified.

fn

a callback function which returns the new content of the note synchronously.

options?: DataWriteOptions

write options.

Returns

Promise<string>

string - the text value of the note that was written.

Example

app.vault.process(file, (data) => {
return data.replace('Hello', 'World');
});

read()

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.

Parameters

file: TFile

Returns

Promise<string>


readBinary()

readBinary(file): Promise<ArrayBuffer>

Read the content of a binary file stored inside the vault.

Parameters

file: TFile

Returns

Promise<ArrayBuffer>


readConfigJson()

readConfigJson(config): Promise<null | object>

Read a config file from the vault and parse it as JSON

Parameters

config: string

Name of config file

Returns

Promise<null | object>


readJson()

readJson(path): Promise<null | object>

Read a config file (full path) from the vault and parse it as JSON

Parameters

path: string

Full path to config file

Returns

Promise<null | object>


readPluginData()

readPluginData(path): Promise<null | object>

Read a plugin config file (full path relative to vault root) from the vault and parse it as JSON

Parameters

path: string

Full path to plugin config file

Returns

Promise<null | object>


readRaw()

readRaw(path): Promise<string>

Read a file from the vault as a string

Parameters

path: string

Path to file

Returns

Promise<string>


rename()

rename(file, newPath): Promise<void>

Rename or move a file. To ensure links are automatically renamed, use FileManager.renameFile instead.

Parameters

file: TAbstractFile

the file to rename/move

newPath: string

vault absolute path to move file to.

Returns

Promise<void>


setConfig()

setConfig(key, value): void

Set value of config by key

Parameters

key: ConfigItem

Key of config value

value: unknown

Value to set

Returns

void


setConfigDir()

setConfigDir(configDir): void

Set where the config files are stored (relative to vault root)

Parameters

configDir: string

Path to config files

Returns

void


trash()

trash(file, system): Promise<void>

Tries to move to system trash. If that isn’t successful/allowed, use local trash

Parameters

file: TAbstractFile

The file or folder to be deleted

system: boolean

Set to false to use local trash by default.

Returns

Promise<void>


trigger()

trigger(name, …data): void

Parameters

name: string

• …data: unknown[]

Returns

void

Inherited from

Events.trigger


tryTrigger()

tryTrigger(evt, args): void

Parameters

evt: EventRef

args: unknown[]

Returns

void

Inherited from

Events.tryTrigger


writeConfigJson()

writeConfigJson(config, data): Promise<void>

Write a config file to disk

Parameters

config: string

Name of config file

data: object

Data to write

Returns

Promise<void>


writeJson()

writeJson(path, data, pretty?): Promise<void>

Write a config file (full path) to disk

Parameters

path: string

Full path to config file

data: object

Data to write

pretty?: boolean

Whether to insert tabs or spaces

Returns

Promise<void>


writePluginData()

writePluginData(path, data): Promise<void>

Write a plugin config file (path relative to vault root) to disk

Parameters

path: string

data: object

Returns

Promise<void>


recurseChildren()

static recurseChildren(root, cb): void

Parameters

root: TFolder

cb

Returns

void