Vim: Creating parent directories on save

前端 未结 6 1819
一生所求
一生所求 2020-12-07 08:32

If I invoke vim foo/bar/somefile but foo/bar don\'t already exist, Vim refuses to save.

I know I could switch to a shell or do :!mkdi

6条回答
  •  半阙折子戏
    2020-12-07 09:11

    Based on the suggestions to my question, here's what I ended up with:

    function WriteCreatingDirs()
        execute ':silent !mkdir -p %:h'
        write
    endfunction
    command W call WriteCreatingDirs()
    

    This defines the :W command. Ideally, I'd like to have all of :w!, :wq, :wq!, :wall etc work the same, but I'm not sure if it's possible without basically reimplementing them all with custom functions.

提交回复
热议问题