Skip to main content
3 of 3
deleted 20 characters in body
Maxim Kim
  • 13.9k
  • 2
  • 20
  • 48

If you look into :h 'backupdir', you will find in the description:

    - For Unix and Win32, if a directory ends in two path separators "//",
      the backup file name will be built from the complete path to the
      file with all path separators changed to percent '%' signs. This
      will ensure file name uniqueness in the backup directory.
      On Win32, it is also possible to end with "\\".  However, When a
      separating comma is following, you must use "//", since "\\" will
      include the comma in the file name. Therefore it is recommended to
      use '//', instead of '\\'.

Thus, if you add // at the end of the path, it should behave the same as 'undodir':

set backupdir=~/.cache/vim/backup// |call mkdir(&backupdir, 'p', 0o700)

One example of setting up directories:

vim9script

g:vimdata = $'{has("win32") ? expand("$APPDATA") : expand("~/.config")}/vim-data'
if !isdirectory(g:vimdata) | mkdir(g:vimdata, "p") | endif

&directory = expand($'{g:vimdata}/swap/')
&backupdir = expand($'{g:vimdata}/backup//')
&undodir = expand($'{g:vimdata}/undo//')
if !isdirectory(&undodir)   | mkdir(&undodir, "p")   | endif
if !isdirectory(&backupdir) | mkdir(&backupdir, "p") | endif
if !isdirectory(&directory) | mkdir(&directory, "p") | endif

set backup
set undofile

Here, I have edited 2 readme.md files, one in ~/temp/hello/ and another in ~/temp/world/ directories:

enter image description here

Maxim Kim
  • 13.9k
  • 2
  • 20
  • 48