1

Is there any way to save these files to automatically created subdirs? Google gave me no answers.

The reason is simple- I work on several computers which I keep synced via git, so having these files in the same directory structure is practical to me. If all these files could be in a single subfolder of the repo's root for extra neatness it'd be great, but just having a /.vimfiles folder in each directory wouldn't be bad either.

4
  • Are you wanting to sync the swap files, or have git ignore them? Theres a difference in how to accomplish one or the other.
    – D. Ben Knoble
    Jul 10, 2018 at 23:49
  • I want to keep them synced, but at the time of making this ask forgot that files in .git are ignored automatically. Editing to avoid confusion. Jul 11, 2018 at 1:51
  • Out of the box vim won't do the per-repo subdir. Also, when you specify a directory for swapfiles it has to exist. It it were me I'd create a shell script "wrapper" for vim that does these things and alias vim to that. Or perhaps use autocommands (not sure off the top of my head if an event comes early enough to create swap dirs before vim complains).
    – B Layer
    Jul 11, 2018 at 2:44
  • You can actually make the directory in your vimrc before setting; i do this with undodir
    – D. Ben Knoble
    Jul 11, 2018 at 21:58

1 Answer 1

1

Short answer:

Use the mkdir() function to create those subdirectories and then set undodir, backupdir and directory to tell vim to use them. This snippet of code should work just fine:

function! Mkdir(path)
    if !isdirectory(resolve(a:path))
        try
            call mkdir(a:path, "p", 0775)
        catch
            return ''
        endtry
    endif
    return a:path
endfunction

" Find the nearest .git directory
let s:repo_root = ''
let s:cwd = getcwd()
while getcwd() != '/'
    if isdirectory('.git')
        let s:repo_root = getcwd()
        break
    endif
    cd ..
endwhile
exec 'cd '.s:cwd

" If we found a .git/ directory somewhere, create directories there
if s:repo_root != ''
    set undofile
    set backup
    set swapfile

    let s:dirpath = s:repo_root."/.vimfiles"
    let &undodir = Mkdir(s:dirpath."/undo")
    let &backupdir = Mkdir(s:dirpath."/backup")
    let &directory = Mkdir(s:dirpath."/swp")
endif

Long answer

It's probably not a good idea to sync those files with git. Keep in mind that they will be constantly changing, and that will add a lot of clutter to your commits. Also there is no point in syncing .swp files, since the whole point of having .swp files is to avoid having two instances of the editor working on the same file at once. If you are syncing between different machines, these files stop making any sense.

I would suggest you don't put those files under your projects directory, but rather save them in subdirectories inside ~/.vim. In that case you only have to slightly modify the snippet above to remove the .git/ discovery part, and to replace s:dir_path with ~/.vim/

3
  • I don't do constant syncronization -or even use this for actual code- but instead move between my laptop and desktop constantly. Git is the best way I have to keep files synced between both of those and my USB. And keeping .swp files is a better idea than it might seem- remember they act as restoration points if vim closes unexpectedly. Jul 13, 2018 at 19:45
  • @DudeForrealsies I don't see it that way, but whatever works for you. At least I hope the code helps you out
    – Ocab19
    Jul 14, 2018 at 21:24
  • Hm, this seems to only work if I start gvim in the same directory as the file, such as "gvim . 1.md", but it does what I want nonetheless. Jul 17, 2018 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.