Visual Studio Code creates redundant files with the end ~


Sajid Sajid Ansari Asked on Jan 6, 2025
Summary:-

Visual Studio Code creates redundant files with the end ~

For example: index.ts and index.ts~

There doesn't seem to be any extensions related to this.

This has never happened before.

What could be the reason and what is solution for it

I tried switching the autoSave and hot Exit parameters, but nothing changed.

Urgency of Question:-

High Urgency

Skill Level:-

Advanced

Rahul Rahul Verma Commented on Jan 6, 2025

1. Possible Causes

  1. Editor or Extension Backup Settings
    • Some editors (like Emacs or older text editors) append a ~ to backup files.
    • Even if you don’t see any obvious extensions, a recently installed or previously hidden extension (or a system-level tool) might still be creating these files.
  2. Git / Version Control Hooks or Scripts
    • Occasionally, a custom git hook or script can create backup files before committing. Check your .git/hooks folder or any pre-commit scripts for references to backup.
  3. Operating System-Level Backups
    • Certain OS-level or file-system tools might be configured to create backup files. For instance, some file managers or older custom scripts on Linux-based systems can do this.
  4. Team or Project Configuration
    • If you are collaborating on a project, another team member might have committed settings or scripts that generate these backup files.

2. Steps to Diagnose

  1. Temporarily Disable All Extensions
    • In VS Code, press Ctrl + Shift + P (Windows/Linux) or Cmd + Shift + P (Mac), type Extensions: Disable All Installed Extensions, and then reload VS Code.
    • Check if the ~ files are still being created. If not, it’s likely one of your extensions. Re-enable them one by one to find the culprit.
  2. Check for Hidden or Global Extensions
    • Some extensions may be installed at a global or workspace level rather than a user level. Switch to the Workspace tab in the Extensions sidebar to see if something is enabled only for a specific folder/workspace.
  3. Inspect Local or Global Settings
    • Go to File > Preferences > Settings (or press Ctrl + , / Cmd + ,).
    • Search for terms like backup, files.trimTrailingWhitespace, autoSave, hotExit, saveBeforeRun, or createBackup. You might find a setting that automatically creates backup files.
  4. Review Git Hooks
    • If you’re using git, open the .git/hooks directory in your project. Check each script (pre-commit, post-commit, etc.) for lines that copy or rename files with a ~ suffix.
  5. Check System-Level Scripts or Editors
    • If you occasionally open files with Emacs, Vim, or other editors, they might be generating these backups. For instance, Emacs appends ~ by default. Verify you’re not opening the files in Emacs or another terminal-based editor.

3. How to Stop the Creation of ~ Files

  1. Disable or Reconfigure the Culprit Extension
    • If an extension is generating these backups, disable or uninstall it.
    • Alternatively, check its settings to see if you can turn off backup creation.
  2. Disable Backup Features in Other Editors
    • If you also use Emacs, Vim, or Sublime Text, disable or change the backup settings to store backup files elsewhere (e.g., ~/.cache/emacs/backups/).
  3. Use VS Code’s Built-In Auto Save
    • VS Code’s native auto-save does not append ~. Go to File > Preferences > Settings and enable:
json
Copy code
"files.autoSave": "afterDelay"
    • This will auto-save without creating backup files.
  1. Ignore ~ Files in Git
    • Even if you can’t eliminate them entirely, you can at least ensure they’re not tracked by adding a rule in your .gitignore:
Copy code
*~
  1. Manually Remove Existing ~ Files
    • If you have a bunch of these files already, you can safely delete them if you don’t need them as backups. They’re not usually required for compiling or running your app.

4. Why This Happens (Root Cause)

  • Backups Are a Common Practice: Many text editors (especially older or Unix-based ones like Emacs) default to creating backup files for safety.
  • VS Code Typically Doesn’t: Visual Studio Code by itself doesn’t append ~ to backup files. Therefore, the culprit is almost always an external editor, extension, or script.

Do you know the Answer?

Got a question? Ask our extensive community for help. Submit Your Question

Recent Posts