OPEN TO WORK

Available for new opportunities! Let's build something amazing together.

#DevHack: How to rename a file from a VSCode extension

Extensions VSCode Development
post

This post is over a year old, some of this information may be out of date.

Renaming a file is something that we do very often, but in my case, I wanted to do it from within a Visual Studio Code extension. When I first looked at the APIs and available documentation, I found the vscode.executeDocumentRenameProvider command and RenameProvider.

Now, I thought, there must be an easier way, so I started to look around at the APIs available, and luckily there is an easy option.

On the vscode, its workspace namespace, you can access the fs or file system via: vscode.workspace.fs. The fs instance delivers you a couple of valuable methods you could use, like rename.

Show image workspace.fs methods
workspace.fs methods

If you want to rename a file, you just need to use the rename method as follows:

const editor = vscode.window.activeTextEditor;
if (editor) {
await vscode.workspace.fs.rename(editor.document.uri, vscode.Uri.file(newPath), {
overwrite: false
});
}

Related articles

Report issues or make changes on GitHub

Found a typo or issue in this article? Visit the GitHub repository to make changes or submit a bug report.

Comments

Elio Struyf

Solutions Architect & Developer Expert

Loading...

Let's build together

Manage content in VS Code

Present from VS Code