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 usebehave the same thing that you have foras 'undodir' now.:
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
As an exampleHere, I have edited 2 files readme.md
files, one in ~/temp/hello/
and another in ~/temp/world/
directories: