Create An Untitled File In A VS Code Extension

October 2024

This is the extension.ts file that opens a new, untitled file and brings it up in the editor.

(You'll need to change vs-code-auto-typer.autoTypeScript to match whatever you have in your package.json file)

See also this post which is what I want to do most of the time:

[issue: could not generate tlink]

import * as vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {
	const disposable = vscode.commands.registerCommand('vs-code-auto-typer.autoTypeScript', async () => {
    /////////////////////////////////////////////////////
    // Basic open of an untitled file:
		const doc = await vscode.workspace.openTextDocument();
    // you can also add a language to openTextDocument to set up
    // for specific syntax highlighting. For example:
    //
    // const doc = await vscode.workspace.openTextDocument({ language: "python" });
    /////////////////////////////////////////////////////
		await vscode.window.showTextDocument(doc, { preview: false });
	});
	context.subscriptions.push(disposable);
}

export function deactivate() {}
end of line

References

This is where I finally figured out that you can open an untitled file but just skipping the uri when calling openTextDocument

Search for openTextDocument() to see the specifics for it.

The issue that led me to figuring out openTextDocument was the way to go.