Permalink
Browse files

Vim :saveas! creates dir if necessary.

  • Loading branch information...
1 parent aaa45c1 commit 54cc9474b345332cf54cf25b51ddb8a9bd00a0bb @henrik committed Feb 23, 2012
Showing with 24 additions and 1 deletion.
  1. +23 −0 vim/shorts/saveas_bang_to_mkdir_p.vim
  2. +1 −1 vimrc
@@ -0,0 +1,23 @@
+" http://vim.wikia.com/wiki/Replace_a_builtin_command_using_cabbrev
+
+function! s:CommandCabbr(abbreviation, expansion)
+ execute 'cabbr ' . a:abbreviation . ' <c-r>=getcmdpos() == 1 && getcmdtype() == ":" ? "' . a:expansion . '" : "' . a:abbreviation . '"<CR>'
+endfunction
+
+command! -nargs=+ CommandCabbr call <SID>CommandCabbr(<f-args>)
+
+
+" :SaveAs! with a bang creates directories if they don't exist.
+command! -nargs=1 -complete=file -bang SaveAs :call SaveAs(<f-args>, '<bang>')
+" :saveas calls :SaveAs
+CommandCabbr saveas SaveAs
+
+function! SaveAs(filename, bang)
+ if a:bang == "!"
+ let l:dir = fnamemodify(a:filename, ":p:h")
+ if !isdirectory(l:dir)
+ call mkdir(l:dir, "p")
+ end
+ endif
+ exe "saveas" . a:bang . " " . a:filename
+endfunction
View
2 vimrc
@@ -25,4 +25,4 @@ source ~/.vim/config/mappings.vim
source ~/.vim/shorts/edit_rails_locales.vim
source ~/.vim/shorts/focus_toggle.vim
source ~/.vim/shorts/run_html.vim
-
+source ~/.vim/shorts/saveas_bang_to_mkdir_p.vim

0 comments on commit 54cc947

Please sign in to comment.