2

I want to have a backup folder with the unique backups (*~ files) like I have with my undo history. For undo I can use:

set undofile
set undodir=~/.vim/.undo//

to get unique files due to the '//' ending. Is there a way to do the same for my backup files?

set backup
set backupdir=~/.vim/.backup//

just create 'filename~' copies, so they will be overwritten if I work on files with the same name.

I am using Vim 7.4.488

DerWeh
  • 1,721
  • 1
  • 15
  • 26

1 Answers1

2

Try to add

au BufWritePre * let &bex = '-' . strftime("%Y%b%d%X") . '~'

You can also add a file path by

au BufWritePre * let &bex = '-' . substitute(expand('%:p:h'),'[\,/]','%','g') . '-' . strftime("%Y%b%d%X") . '~'


More reading about why the '//' ending doesn't work in the case of 'backupdir' I refer to Ingo Karkat's answer.

Community
  • 1
  • 1
ryuichiro
  • 3,765
  • 1
  • 16
  • 21
  • This is a start but doesn't really help as I suck with vim script. Appending the time makes the files unique as I want, but it is very ugly to identify which backup belongs to which file. A solution would be to use `expand('%:p') . '~'` as `backupext`. How do you replace the delimiters with `'%'`? – DerWeh Oct 29 '16 at 13:53
  • strftime("%X") produces string with ':'. It causes errors on Windows. Also Window file path contains ':' and we need to substitute is also. This command works for me on Windows: au BufWritePre * let &bex = '-' . substitute(expand('%:p:h'),'[\,/,:]','%','g') . '~' – Andrey Epifantsev May 31 '19 at 00:29