Change title of untitled tab in Visual Studio Code
Asked Answered
H

3

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?

Hisakohisbe answered 25/8, 2017 at 8:1 Comment(0)
D
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.

Deitz answered 25/8, 2017 at 8:42 Comment(4)
this is sad. Is there any way I can convince them to make the field mutable?Hisakohisbe
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.Deitz
oh so there's a possibility that tab title is not taken from filename? Could you please confirmHisakohisbe
Seems that ultimately, getPathLabel() is responsible for the editor titles, which indeed uses the file name.Deitz
N
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://static.mcmap.net/file/mcmap/ZG-AbGLDKwftXV-pWS2nZ7-ocVI0bRywWRfQcFyQcC2jaRA/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.

Nigro answered 29/1, 2020 at 17:15 Comment(1)
that's exactly what I was trying to build the extension for.Hisakohisbe
D
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.

Deitz answered 25/8, 2017 at 8:42 Comment(4)
this is sad. Is there any way I can convince them to make the field mutable?Hisakohisbe
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.Deitz
oh so there's a possibility that tab title is not taken from filename? Could you please confirmHisakohisbe
Seems that ultimately, getPathLabel() is responsible for the editor titles, which indeed uses the file name.Deitz
S
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.

Shepley answered 18/8, 2022 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.