Skip to content

Vault

Defined in: obsidian.d.ts:4235

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[]>

Defined in: src/obsidian/augmentations/Events.d.ts:5

Inherited from

Events._


adapter

adapter: DataAdapter

Defined in: obsidian.d.ts:4239


config

config: AppVaultConfig

Defined in: src/obsidian/augmentations/Vault.d.ts:23

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

configDir

configDir: string

Defined in: obsidian.d.ts:4246

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>

Defined in: obsidian.d.ts:4379

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>

Defined in: obsidian.d.ts:4322

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>

Defined in: obsidian.d.ts:4401

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>

Defined in: obsidian.d.ts:4292

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>

Defined in: obsidian.d.ts:4301

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>

Defined in: obsidian.d.ts:4308

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>

Defined in: obsidian.d.ts:4340

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>

Defined in: src/obsidian/augmentations/Vault.d.ts:44

Check whether a path exists in the vault

Parameters

path

string

isCaseSensitive?

boolean

Returns

Promise<boolean>


getAbstractFileByPath()

getAbstractFileByPath(path): null | TAbstractFile

Defined in: obsidian.d.ts:4277

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

Defined in: src/obsidian/augmentations/Vault.d.ts:50

Get an abstract file by path, insensitive to case

Parameters

path

string

Returns

null | TAbstractFile


getAllFolders()

getAllFolders(includeRoot?): TFolder[]

Defined in: obsidian.d.ts:4412

Get all folders in the vault.

Parameters

includeRoot?

boolean

Should the root folder (/) be returned

Returns

TFolder[]


getAllLoadedFiles()

getAllLoadedFiles(): TAbstractFile[]

Defined in: obsidian.d.ts:4406

Get all files and folders in the vault.

Returns

TAbstractFile[]


getConfig()

getConfig(string): unknown

Defined in: src/obsidian/augmentations/Vault.d.ts:61

Get value from config by key

Parameters

string

ConfigItem

Key of config value

Returns

unknown

getConfigFile()

getConfigFile(configFile): string

Defined in: src/obsidian/augmentations/Vault.d.ts:65

Get path to config file (relative to vault root)

Parameters

configFile

string

Returns

string


getDirectParent()

getDirectParent(file): null | TFolder

Defined in: src/obsidian/augmentations/Vault.d.ts:71

Get direct parent of file

Parameters

file

TAbstractFile

File to get parent of

Returns

null | TFolder


getFileByPath()

getFileByPath(path): null | TFile

Defined in: obsidian.d.ts:4261

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[]

Defined in: obsidian.d.ts:4427

Get all files in the vault.

Returns

TFile[]


getFolderByPath()

getFolderByPath(path): null | TFolder

Defined in: obsidian.d.ts:4269

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[]

Defined in: obsidian.d.ts:4422

Get all Markdown files in the vault.

Returns

TFile[]


getName()

getName(): string

Defined in: obsidian.d.ts:4252

Gets the name of the vault.

Returns

string


getResourcePath()

getResourcePath(file): string

Defined in: obsidian.d.ts:4333

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

Parameters

file

TFile

Returns

string


getRoot()

getRoot(): TFolder

Defined in: obsidian.d.ts:4283

Get the root folder of the current vault.

Returns

TFolder


modify()

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

Defined in: obsidian.d.ts:4363

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>

Defined in: obsidian.d.ts:4371

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

Defined in: obsidian.d.ts:1441

Parameters

name

string

callback

(…data) => unknown

Returns

void

Inherited from

Events.off


offref()

offref(ref): void

Defined in: obsidian.d.ts:1445

Parameters

ref

EventRef

Returns

void

Inherited from

Events.offref


on()

Call Signature

on(name, callback, ctx?): EventRef

Defined in: obsidian.d.ts:4435

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

(file) => any

ctx?

any

Returns

EventRef

Inherited from

Events.on

Call Signature

on(name, callback, ctx?): EventRef

Defined in: obsidian.d.ts:4440

Called when a file is modified.

Parameters
name

"modify"

callback

(file) => any

ctx?

any

Returns

EventRef

Inherited from

Events.on

Call Signature

on(name, callback, ctx?): EventRef

Defined in: obsidian.d.ts:4445

Called when a file is deleted.

Parameters
name

"delete"

callback

(file) => any

ctx?

any

Returns

EventRef

Inherited from

Events.on

Call Signature

on(name, callback, ctx?): EventRef

Defined in: obsidian.d.ts:4450

Called when a file is renamed.

Parameters
name

"rename"

callback

(file, oldPath) => any

ctx?

any

Returns

EventRef

Inherited from

Events.on

Call Signature

on(name, callback, ctx?): EventRef

Defined in: src/obsidian/augmentations/Vault.d.ts:83

Called whenever any of Obsidian’s settings are changed

Parameters
name

"config-changed"

callback

() => void

ctx?

unknown

Returns

EventRef

Inherited from

Events.on


process()

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

Defined in: obsidian.d.ts:4394

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

Parameters

file

TFile

the file to be read and modified.

fn

(data) => string

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>

Defined in: obsidian.d.ts:4315

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>

Defined in: obsidian.d.ts:4327

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

Parameters

file

TFile

Returns

Promise<ArrayBuffer>


readConfigJson()

readConfigJson(config): Promise<null | object>

Defined in: src/obsidian/augmentations/Vault.d.ts:91

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>

Defined in: src/obsidian/augmentations/Vault.d.ts:97

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>

Defined in: src/obsidian/augmentations/Vault.d.ts:103

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>

Defined in: src/obsidian/augmentations/Vault.d.ts:109

Read a file from the vault as a string

Parameters

path

string

Path to file

Returns

Promise<string>


rename()

rename(file, newPath): Promise<void>

Defined in: obsidian.d.ts:4355

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

Defined in: src/obsidian/augmentations/Vault.d.ts:132

Set value of config by key

Parameters

key

ConfigItem

Key of config value

value

unknown

Value to set

Returns

void


setConfigDir()

setConfigDir(configDir): void

Defined in: src/obsidian/augmentations/Vault.d.ts:138

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>

Defined in: obsidian.d.ts:4347

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

Defined in: obsidian.d.ts:1449

Parameters

name

string

data

unknown[]

Returns

void

Inherited from

Events.trigger


tryTrigger()

tryTrigger(evt, args): void

Defined in: obsidian.d.ts:1453

Parameters

evt

EventRef

args

unknown[]

Returns

void

Inherited from

Events.tryTrigger


writeConfigJson()

writeConfigJson(config, data): Promise<void>

Defined in: src/obsidian/augmentations/Vault.d.ts:149

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>

Defined in: src/obsidian/augmentations/Vault.d.ts:157

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>

Defined in: src/obsidian/augmentations/Vault.d.ts:161

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

Defined in: obsidian.d.ts:4417

Parameters

root

TFolder

cb

(file) => any

Returns

void