#DevHack: How to rename a file from a VSCode extension
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
.

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
#DevHack: Open custom VSCode WebView panel and focus input
#DevHack: language-specific settings in a VSCode extension
Get to know how you can set language-specific settings straight from within the code of your Visual Studio Code extension.
#DevHack: check how your VSCode extension is running
This article describes how you can check if your VSCode extension is running in development, production, or test mode.
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
Let's build together
Manage content in VS Code
Present from VS Code