让git忽略vim在所有目录中生成的临时文件的正确方法是什么(无论是跨系统全局的还是单个项目的本地的)?


当前回答

或者,您可以配置vim以将交换文件保存到单独的位置, 例如,在你的.vimrc文件中添加如下代码行:

set backupdir=$TEMP//
set directory=$TEMP//

有关更多信息,请参阅这个vim技巧。

其他回答

我还建议忽略以下文件:

.*.swp
.*.swo

因为您可能有以.swp结尾的文件

这应该只针对每个用户进行,而不是针对每个存储库。如果Joe使用emacs,他希望忽略emacs备份文件,但是Betty(她使用vi)希望忽略vi备份文件(在许多情况下,它们是相似的,但是存在大约24,893个常见的编辑器,试图忽略所有不同的备份扩展名是非常荒谬的)。

换句话说,不要把任何东西放在。gitignore或core中。在$GIT_DIR/config中排除。把信息放在$HOME/。改用Gitconfig(如nonopolonia建议的使用——global。)注意,“全局”指的是每个用户,而不是每个系统。

如果您希望为所有用户在整个系统中进行配置(实际上并不是这样),那么您将需要一种不同的机制。(可能在初始化存储库之前设置模板。)

在我的情况下,临时文件已经被之前的操作提交了,所以修改.gitignore不会影响那些提交的文件…,你必须git rm files_to_be_ignored——先缓存,然后提交,然后DONE。

# VIM: Temperory files
*~

# VIM: Swap-files
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]

# VIM: Commands :cs, :ctags
tags
cscope.*

# VIM session
Session.vim

# VIM: netrw.vim: Network oriented reading, writing, browsing (eg: ftp scp) 
.netrwhist

The name of the swap file is normally the same as the file you are editing, with the extension ".swp". On Unix, a '.' is prepended to swap file names in the same directory as the edited file. This avoids that the swap file shows up in a directory listing. On MS-DOS machines and when the 'shortname' option is on, any '.' in the original file name is replaced with '_'. If this file already exists (e.g., when you are recovering from a crash) a warning is given and another extension is used, ".swo", ".swn", etc. An existing file will never be overwritten. The swap file is deleted as soon as Vim stops editing the file. The replacement of '.' with '_' is done to avoid problems with MS-DOS compatible filesystems (e.g., crossdos, multidos).

http://vimdoc.sourceforge.net/htmldoc/recover.html

http://www.vim.org/scripts/script.php?script_id=1075

在“git commit”之前退出vim。

要使vim使用其他文件夹作为备份文件(例如/tmp):

set bdir-=.
set bdir+=/tmp

要使vim停止使用当前文件夹的。swp文件:

set dir-=.
set dir+=/tmp

使用-=,+=通常会很好,因为vim对bdir, dir有其他默认值,我们不想全部清除。查看vim帮助以获得更多关于bdir, dir的信息:

:h bdir
:h dir