Last active
December 30, 2020 13:54
Revisions
-
George-B revised this gist
Dec 30, 2020 . 1 changed file with 3 additions and 5 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) execute "autocmd MkDir BufWritePre <buffer>" \ "call mkdir(" . a:path . ", 'p')" \ "| autocmd! MkDir * <buffer>" endif endfunction -
George-B revised this gist
May 22, 2020 . 1 changed file with 8 additions and 9 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(path) abort if !isdirectory(a:path) call mkdir(a:path, 'p') endif endfunction augroup MkDir autocmd! 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(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(expand('<afile>:p:h')) augroup END ``` -
George-B revised this gist
May 22, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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') \ | unlet b:path \ | autocmd! MkDir * <buffer> endif endfunction -
George-B revised this gist
May 22, 2020 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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> \ call mkdir(b:path, 'p') \ | autocmd! MkDir * <buffer> \ | unlet b:path endif endfunction -
George-B revised this gist
May 22, 2020 . 1 changed file with 8 additions and 9 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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> ++once call mkdir(b:path, 'p') | unlet b:path endif endfunction -
George-B created this gist
May 22, 2020 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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.