Skip to content

Instantly share code, notes, and snippets.

@g0xA52A2A
Last active December 30, 2020 13:54

Revisions

  1. @George-B George-B revised this gist Dec 30, 2020. 1 changed file with 3 additions and 5 deletions.
    8 changes: 3 additions & 5 deletions Vim_create_path.md
    Original file line number Diff line number Diff line change
    @@ -30,11 +30,9 @@ prior to any attempt to write it.
    ```vim
    function! MkDir(path) abort
    if !isdirectory(a:path)
    let b:path = a:path
    autocmd MkDir BufWritePre <buffer>
    \ call mkdir(b:path, 'p')
    \ | unlet b:path
    \ | autocmd! MkDir * <buffer>
    execute "autocmd MkDir BufWritePre <buffer>"
    \ "call mkdir(" . a:path . ", 'p')"
    \ "| autocmd! MkDir * <buffer>"
    endif
    endfunction
  2. @George-B George-B revised this gist May 22, 2020. 1 changed file with 8 additions and 9 deletions.
    17 changes: 8 additions & 9 deletions Vim_create_path.md
    Original file line number Diff line number Diff line change
    @@ -4,16 +4,15 @@ A common method to ensure the path exists is to use an `autocmd` to
    check if the parent path exists and if not create it.

    ```vim
    function! MkDir() abort
    let path = expand('<afile>:p:h')
    if !isdirectory(path)
    call mkdir(path, 'p')
    function! MkDir(path) abort
    if !isdirectory(a:path)
    call mkdir(a:path, 'p')
    endif
    endfunction
    augroup MkDir
    autocmd!
    autocmd BufWritePre * call MkDir()
    autocmd BufWritePre * call MkDir(expand('<afile>:p:h'))
    augroup END
    ```

    @@ -29,9 +28,9 @@ path exists, if not set up a buffer local one shot `autocmd` to create the path
    prior to any attempt to write it.

    ```vim
    function! MkDir() abort
    let b:path = expand('<afile>:p:h')
    if !isdirectory(b:path)
    function! MkDir(path) abort
    if !isdirectory(a:path)
    let b:path = a:path
    autocmd MkDir BufWritePre <buffer>
    \ call mkdir(b:path, 'p')
    \ | unlet b:path
    @@ -41,7 +40,7 @@ endfunction
    augroup MkDir
    autocmd!
    autocmd BufNewFile * call MkDir()
    autocmd BufNewFile * call MkDir(expand('<afile>:p:h'))
    augroup END
    ```

  3. @George-B George-B revised this gist May 22, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Vim_create_path.md
    Original file line number Diff line number Diff line change
    @@ -34,8 +34,8 @@ function! MkDir() abort
    if !isdirectory(b:path)
    autocmd MkDir BufWritePre <buffer>
    \ call mkdir(b:path, 'p')
    \ | autocmd! MkDir * <buffer>
    \ | unlet b:path
    \ | autocmd! MkDir * <buffer>
    endif
    endfunction
  4. @George-B George-B revised this gist May 22, 2020. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion Vim_create_path.md
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,10 @@ prior to any attempt to write it.
    function! MkDir() abort
    let b:path = expand('<afile>:p:h')
    if !isdirectory(b:path)
    autocmd MkDir BufWritePre <buffer> ++once call mkdir(b:path, 'p') | unlet b:path
    autocmd MkDir BufWritePre <buffer>
    \ call mkdir(b:path, 'p')
    \ | autocmd! MkDir * <buffer>
    \ | unlet b:path
    endif
    endfunction
  5. @George-B George-B revised this gist May 22, 2020. 1 changed file with 8 additions and 9 deletions.
    17 changes: 8 additions & 9 deletions Vim_create_path.md
    Original file line number Diff line number Diff line change
    @@ -20,20 +20,19 @@ augroup END
    This is effective but hardly efficient. Every time we write any buffer
    we check for the existence of the parent path.

    Firstly the `BufWritePre` event can be swapped for the `BufNewFile`
    event, the later being triggered far less often. However this means if
    we open a new file but choose to abandon it (not writing it) we now have
    inadvertently created a path on disk. Instead of immediately creating
    the path we want to defer it to when/if the buffer is written. So upon
    editing a new file check if its parent path exists, if it doesn't set up
    a buffer local `autocmd` to create the path prior to any attempt to
    write it and then clear said buffer local `autocmd`.
    Firstly the `BufWritePre` event can be swapped for the `BufNewFile` event, the
    later being triggered far less often. However this means if we open a new file
    but choose to abandon it (not writing it) we now have inadvertently created
    a path on disk. Instead of immediately creating the path we want to defer it to
    when/if the buffer is written. So upon editing a new file check if its parent
    path exists, if not set up a buffer local one shot `autocmd` to create the path
    prior to any attempt to write it.

    ```vim
    function! MkDir() abort
    let b:path = expand('<afile>:p:h')
    if !isdirectory(b:path)
    autocmd MkDir BufWritePre <buffer> call mkdir(b:path, 'p') | autocmd! MkDir * <buffer>
    autocmd MkDir BufWritePre <buffer> ++once call mkdir(b:path, 'p') | unlet b:path
    endif
    endfunction
  6. @George-B George-B created this gist May 22, 2020.
    46 changes: 46 additions & 0 deletions Vim_create_path.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    Vim will not create parent directories if they do not exist on write.

    A common method to ensure the path exists is to use an `autocmd` to
    check if the parent path exists and if not create it.

    ```vim
    function! MkDir() abort
    let path = expand('<afile>:p:h')
    if !isdirectory(path)
    call mkdir(path, 'p')
    endif
    endfunction
    augroup MkDir
    autocmd!
    autocmd BufWritePre * call MkDir()
    augroup END
    ```

    This is effective but hardly efficient. Every time we write any buffer
    we check for the existence of the parent path.

    Firstly the `BufWritePre` event can be swapped for the `BufNewFile`
    event, the later being triggered far less often. However this means if
    we open a new file but choose to abandon it (not writing it) we now have
    inadvertently created a path on disk. Instead of immediately creating
    the path we want to defer it to when/if the buffer is written. So upon
    editing a new file check if its parent path exists, if it doesn't set up
    a buffer local `autocmd` to create the path prior to any attempt to
    write it and then clear said buffer local `autocmd`.

    ```vim
    function! MkDir() abort
    let b:path = expand('<afile>:p:h')
    if !isdirectory(b:path)
    autocmd MkDir BufWritePre <buffer> call mkdir(b:path, 'p') | autocmd! MkDir * <buffer>
    endif
    endfunction
    augroup MkDir
    autocmd!
    autocmd BufNewFile * call MkDir()
    augroup END
    ```

    This checks the path once and only creates it (if needed) on write.