在 Linux 使用 Vim/Vi 編輯文件時,若會話異常中斷(SSH 斷開、終端關閉、系統崩潰、殺進程),Vim 會在原文件同目錄下創建 .swp 交換文件保存未寫入磁碟的修改。下次打開同一文件時,Vim 會提示檢測到交換文件,提供恢復、刪除、編輯、放棄等選項。本文詳細講解 .swp 文件機制、清理方法、恢復流程與預防最佳實踐。
.文件名.swp(隱藏文件)。.swp。
E325: ATTENTION
Found a swap file by the name ".test.txt.swp"
owned by: user dated: Mon Jan 25 10:00:00 2025
file name: ~user/test.txt
modified: YES
user name: user host name: localhost
process ID: 12345 (still running)
While opening file "test.txt"
dated: Mon Jan 25 09:50:00 2025
(1) Another program may be editing the same file. If this is the case,
be careful not to end up with two different instances of the same
file when making changes. Quit, or continue with caution.
(2) An edit session for this file crashed.
If this is the case, use ":recover" to recover the changes (see :help recovery).
If you did this already, delete the swap file ".test.txt.swp" to avoid this message.
Swap file ".test.txt.swp" already exists!
[O]pen Read-Only, [E]dit anyway, [R]ecover, [D]elete it, [Q]uit, [A]bort:
| 鍵 | 動作 | 適用場景 |
|---|---|---|
R / r | 恢復:從 .swp 恢復未保存修改 | 確認原進程已結束、需要找回修改內容 |
D / d | 刪除:刪除 .swp 文件,正常編輯 | 確認不需要恢復內容、或已手動備份、或原進程仍在運行但你要強制接管 |
E / e | 仍編輯:忽略 .swp,直接編輯當前磁碟版本 | 原進程仍在運行,你只想查看/複製內容,不保存修改 |
O / o | 只讀打開:以只讀模式打開磁碟版本 | 原進程仍在運行,你只需參考內容 |
Q / q | 退出:放棄打開 | 不確定情況,先退出排查 |
A / a | 中止:同退出,但腳本中可捕獲 | 腳本調用 Vim 時 |
# 確認原進程不存在
ps -ef | grep vim | grep test.txt
# 或
lsof /path/to/test.txt
# 刪除
rm -f /path/to/.test.txt.swp
#!/bin/bash
# cleanup_swp.sh: 清理超過 24 小時的 .swp 文件 (避免誤刪正在編輯的)
TARGET_DIRS=("/home" "/opt" "/data" "/tmp")
MAX_AGE_HOURS=24
for dir in "${TARGET_DIRS[@]}"; do
find "$dir" -name "*.swp" -type f -mmin +$((MAX_AGE_HOURS * 60)) -exec ls -lh {} \; 2>/dev/null | \
while read perms links owner group size month day time file; do
# 額外檢查: 原文件是否被進程打開
orig_file="${file%.swp}"
orig_file="${orig_file#.}" # 去掉開頭的 .
if ! lsof "$orig_file" >/dev/null 2>&1; then
echo "清理孤立 swap: $file (原文件: $orig_file)"
rm -f "$file"
else
echo "跳過活躍 swap: $file (原文件正在被編輯)"
fi
done
done
find . -name "*.swp" -type f -delete
# 或顯式確認
find . -name "*.swp" -type f -exec rm -iv {} +
# 方法一: Vim 內建恢復 (推薦)
vim -r test.txt
# 或打開文件時選 R
# 方法二: 手動提取 (不啟動 Vim 界面)
vim -r -c "wq" test.txt # 直接恢復並保存退出
# 方法三: 使用 vim 專用恢復工具 (極少見)
# 從 .swp 導出純文本 (需解析二進制格式, 非標準)
# ~/.vimrc
set directory=~/.vim/swap//
# 雙斜線 // 表示文件名包含完整路徑編碼,避免同名文件衝突
# 需預先創建目錄
mkdir -p ~/.vim/swap
# ~/.vimrc
set updatetime=1000 # 1 秒無操作寫入 swap (預設 4000ms)
set updatecount=100 # 每 100 字符寫入 swap (預設 200)
# ~/.vimrc
set undofile
set undodir=~/.vim/undo//
mkdir -p ~/.vim/undo
# :w 後仍可用 u 撤銷歷史修改,跨會話生效
# 使用 auto-save 插件或
au CursorHold,CursorHoldI * silent! update
# 正常模式/插入模式停頓時自動 :update (僅修改時寫入)
| 概念 | 用途 | 管理命令 | 關鍵特征 |
|---|---|---|---|
| Vim .swp 交換文件 | 編輯器會話崩潰恢復 | `rm .file.swp`, `vim -r` | 用戶級、文件級、臨時、文本編輯專用 |
| 系統 Swap 分區/文件 | 虛擬記憶體擴展 (物理 RAM 不足時換頁) | `swapon`, `swapoff`, `mkswap`, `free`, `vmstat` | 系統級、塊設備/文件、持久、內核管理 |
本文討論的是 **Vim 編輯器交換文件 (.swp)**,非系統交換空間。系統 Swap 管理請參考 `swapon -s`、`/etc/fstab`、`vm.swappiness` 等。

Vim .swp 文件是編輯器崩潰恢復的安全網,也是項目目錄常見的「垃圾文件」來源。建立「異常中斷必恢復、正常結束自動清理、集中存儲目錄、持久化撤銷替代、定期巡檢清理」完整管理體系,配合 `.swp 掃描清理,開發規範強制 `set directory` 集中管理,讓交換文件隱形護航而不成雜物。