You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何阻止.swp文件生成?解决无法删除及暂存该文件的问题

Alright, let’s tackle your .swp file questions head-on—these are super common when working with Vim, so I’ll cover the "why" first, then fix both your issues.

What are .swp files, anyway?

.swap files (shortened to .swp) are Vim’s built-in safety net. When you open a file to edit, Vim creates this temporary file to save your unsaved changes in case something goes wrong—like your terminal crashing, SSH connection dropping, or you accidentally closing Vim without saving. Normally, when you exit Vim properly (with :wq or :q), it deletes the .swp file automatically. But if Vim exits unexpectedly, the .swp file sticks around.

Issue 1: Can’t delete or stage .swp files

If you’re stuck unable to remove these files or stage them in Git, here’s what’s going on and how to fix it:

  • Vim might still be running in the background: Sometimes Vim can hang (especially if you disconnected from a remote session without exiting properly). Check for active Vim processes with:
    ps aux | grep vim
    
    Look for the process linked to your file, then kill it with kill <PID> (replace <PID> with the number from the output). Once the process is gone, you should be able to delete the .swp file with rm .filename.swp.
  • Permission issues: If you edited the file with sudo, the .swp file might be owned by root. Use sudo rm .filename.swp to delete it in that case.
  • Want to recover unsaved changes?: If you open the original file in Vim, it’ll detect the .swp file and give you options. Press R to recover your changes, then save and exit normally—Vim will automatically delete the .swp file afterward.
  • Staging in Git? Don’t bother: .swp files are temporary, so you should add them to your .gitignore instead. Just open .gitignore and add a line:
    *.swp
    
    Commit the .gitignore file, and Git will ignore all .swp files going forward.
Issue 2: How to stop .swp files from being generated

Before we dive in, a quick heads-up: Disabling swap files means you lose Vim’s crash recovery feature. If you’re okay with that, here are your options:

  • Temporary disable for a single edit: Start Vim with the -n flag (short for "no swap file"):
    vim -n yourfile.txt
    
  • Permanent disable for all Vim sessions: Edit your Vim config file (usually ~/.vimrc; if it doesn’t exist, create it). Add this line:
    set noswapfile
    
    Save the file, and the next time you open Vim, it won’t create any .swp files.
  • Middle ground: Move .swp files to a single directory: If you want to keep recovery but don’t want .swp files cluttering your project folders, set a dedicated swap directory in .vimrc:
    set directory=$HOME/.vim/swapfiles//
    
    The double slash at the end tells Vim to use unique filenames for swap files, so you won’t get conflicts across projects. Just create the ~/.vim/swapfiles directory first with mkdir -p ~/.vim/swapfiles.

内容的提问来源于stack exchange,提问作者ark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:41:35