Vim编辑.sh文件内容丢失如何恢复?遇E297/E305报错求助
Recovering Your Lost Shell Script After Vim's E297 Error
Hey there, let’s work through getting your lost script content back step by step. Based on the files you mentioned, here are the most promising avenues to try:
1. Start with the Default Backup File (.sh~)
Vim automatically creates a backup of your file (if the backup setting was turned on) using the .sh~ suffix. This is the easiest first check:
- Open the backup directly in Vim:
vim .sh~ - If this has your missing content, copy it back to your original script. From the command line, you can do this quickly with:
(Replacecat .sh~ > your_script.shyour_script.shwith the actual name of your file.)
2. Recover from Swap File Fragments (.su~, .sv~, etc.)
Those weirdly named .su~, .sv~, .sw~, .sy~, .sz~ files are partial swap file fragments—Vim created them when it hit that E297 write error and couldn’t save the full swap file. You can try to pull content from each of these:
- For each fragment file, run Vim in recovery mode targeting that specific file:
Repeat this for everyvim -r .su~.sX~file you have (swap out.su~for.sv~,.sw~, etc.). - When you run this command, Vim will attempt to reconstruct the file from the fragment. Check if the recovered text matches what you lost.
- If you find a fragment with valid content, save it to your original script by running
:w your_script.shinside Vim.
3. Try System-Level Recovery Tools (If All Else Fails)
If the above steps don’t turn up your content, check if your system has built-in recovery options:
- On Linux, tools like
extundelete(for ext filesystems) might help recover overwritten/deleted files, but this only works if the disk blocks holding your script haven’t been reused yet. - On macOS, if you have Time Machine enabled, you can restore a previous version of the file from a snapshot.
- On Windows, System Restore or File History could have a backup of your script.
Why Your Initial Attempts Didn’t Work
- The
:ucommand failed because saving the empty file overwrote Vim’s undo history—once you save, you can’t undo past that point. - The
:recovercommand looked for Vim’s standard swap file (usually named.your_script.sh.swp), which seems to be gone. The.sX~files are not the standard swap file, so you have to specify them directly withvim -r.
内容的提问来源于stack exchange,提问作者Nima
相关产品推荐
相关产品推荐

