解决了Vim无法支持`backupdir`完整路径的问题

时间:2016-06-02 16:47:42

标签: vim

最近我试图切换到在工作目录以外的目录中备份和交换文件。使用swp文件,您可以在路径末尾添加双斜杠:

+

但是,这不适用于set directory=~/.vim/swp// 。然而,我找到了一个解决方法的答案:

backupdir

关于此解决方法,我有几个问题:

  • 我知道它会在autocmd BufWritePost * :execute ':w! ' ."$HOME/.vim/backups/" . escape(substitute(expand('%:p'), "/", "%", "g"), "%") 中保存文件,但 是如何执行此操作的?
  • 如何修改它以便将文件保存为$HOME/.vim/backups/[filename with path](差异为结尾)
  • 是否可以重新格式化此命令,使其不会越过首选的80个字符?

由于

1 个答案:

答案 0 :(得分:1)

我的解决方法是

" this is a default. to be updated per file type
autocmd BufNewFile,BufNew,BufRead * let b:buDir = "/work/BACKUP/vimBackups/all/"

然后,在每个文件类型的基础上我做了

let b:buDir = "/work/BACKUP/vimBackups/text/"

  let b:buDir = "/work/BACKUP/vimBackups/coding/"

然后,我在每个出口处调用我的备份

" the backup command
" currently, overwrite whatever is there: a new file will be made each second
" if necessary
autocmd VimLeavePre,BufWritePre * silent execute ":write! >> ".b:buDir."vim_bu_".strftime('%Y-%b-%d__%H:%M:%S')."_".b:buFilename.".bu.txt "

然后,在每日清理脚本中,从cron(anacron)运行,我删除了我的旧备份

# vim backups older than a month old
find $VIM_BU_DIR -not -newerat "1 month ago" -delete    ;