Skip to content

VS Code: Close Editor When File is Deleted or Renamed

VS Code: Close Editor When File is Deleted or Renamed

If you’ve ever deleted or renamed a file while it was still open in the editor, you’ve most likely noticed that the tab doesn’t close automatically.

If you like a clean workspace with no dangling tabs, then this post is for you.

I will show you a VS Code setting that, when enabled, will automatically close the tab if the file is deleted or renamed.

📹 Prefer a video tutorial instead? I got you covered:

Close on File Delete

  1. Press CTRL + , (comma) or CMD + , on macOS to open the Settings
  2. Search for workbench.editor.closeOnFileDelete and enable it

You can also directly modify your settings.json file by adding the following line:

"workbench.editor.closeOnFileDelete": true,

Now, whenever you delete or rename a file from disk, the editor tab closes immediately.

ℹ️ It’s important to note that as long as the file has unsaved changes, VS Code will not close the tab, no matter if the setting is enabled or not. This is a data protection mechanism that cannot be overridden.

Special Case

I want to share a special case where VS Code fails to close the tab.

Create a new file and call it test with lowercase letters. Then rename the file on your disk (not from VS Code) to TEST with uppercase letters.

If you’re on Windows or macOS, you will notice that VS Code didn’t close the tab. That’s because Windows and macOS file systems are case-insensitive, and renaming the file from test to TEST isn’t treated as a real path change, just a metadata update.

On the other hand, on Linux, most file systems are case-sensitive. test and TEST are completely different files, so when you rename the file, VS Code sees the old file as deleted, which triggers the tab to close.


Happy coding đź’ś


Let me know what you think about this article in the comments section below.

If you find this article helpful, please share it with others and subscribe to the blog to support me, and receive a bi-monthly-ish e-mail notification on my latest articles.   
  

Comments