10

I'm building a VS Code extension which includes changing the name/title of untitled-1 tab (unsaved file).
I tried running the below code in debugger console of extension but it didn't reflect in the editor:

vscode.workspace.textDocuments[0].fileName="myFile"

Is it not possible or am I missing something?

Gama11
  • 31,714
  • 9
  • 78
  • 100
GorvGoyl
  • 42,508
  • 29
  • 229
  • 225

3 Answers3

13

It is still (Q1 2020) not possible, but the next VSCode 1.42 will name its Untitled editors differently.

Untitled editors in VS Code are text buffers that have not yet been saved to disk.
You can leave them open for as long as you like and all text content is stored and restored between restarts.

Untitled editors were given generic names such as Untitled-1 and counting upwards.
In this release, untitled editors will use the content of the first line of the document for the editor title, and include the generic name as part of the description:

https://media.githubusercontent.com/media/microsoft/vscode-docs/vnext/release-notes/images/1_42/untitled-title2.gif

Note: If the first line is empty or does not contain any words, the title will fall back to Untitled_* as before.

So while you cannot set the title yourself (still readonly fileName), technically... changing the first line of that file would be enough to change the title of said "Untitled" editor.


With VSCode 1.43 (Q1 2020), a new setting workbench.editor.untitled.labelFormat allows to control whether untitled editors should use the contents as title or not.
Possible values are content or name.
Configure 'workbench.editor.untitled.labelFormat': 'name' to get the previous behavior back where untitled editors would have a short title, such as Untitled-1.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
5

It's not possible - if you check out the source code for the API definition in vscode.d.ts, you'll see that fileName is declared as readonly:

export interface TextDocument {
    // ...
    readonly fileName: string;
    // ...
}

Unfortunately, it seems that the readonly attribute isn't reflected in the API docs on the website.

Gama11
  • 31,714
  • 9
  • 78
  • 100
  • this is sad. Is there any way I can convince them to make the field mutable? – GorvGoyl Aug 25 '17 at 13:46
  • Seems unlikely, considering all the other properties are read-only as well. Besides, I doubt think `fileName` directly corresponds to what ends up being shown in the tab title. – Gama11 Aug 25 '17 at 14:01
  • oh so there's a possibility that tab title is not taken from filename? Could you please confirm – GorvGoyl Aug 25 '17 at 14:05
  • Seems that ultimately, [`getPathLabel()`](https://github.com/Microsoft/vscode/blob/1.15.1/src/vs/base/common/labels.ts#L31) is responsible for the editor titles, which indeed uses the file name. – Gama11 Aug 25 '17 at 15:09
0

This mainly happens if we create a new file in the OPEN EDITORS section, thus they appear as unsaved. To prevent this, create a folder for storing your files, and then in that folder, create your new file then it will show options to name it, also you can add a file type extension like .cpp. TIP: vsc-rename-files extension to rename your files.