Saving files in nonexistent directories with vim

Quite often you may find yourself editing a file in a nonexistent directory.

If you’re a Rails developer, you may have used:

:Rview show.html.erb

This creates the show page for the current controller you’re working on, however the app/views/controller directory may not exist yet, and Vim will not let you save a file in a nonexistent directory. So this what you do:

:!mkdir %:p:h

:h expand to the see the meaning of the modifiers used if you’re interested.

An aside, this is what I previously used:

augroup vimrc-auto-mkdir
  autocmd!
  autocmd BufWritePre * call s:auto_mkdir(expand('<afile>:p:h'), v:cmdbang)
  function! s:auto_mkdir(dir, force)
    if !isdirectory(a:dir)
          \   && (a:force
          \       || input("'" . a:dir . "' does not exist. Create? [y/N]") =~? '^y\%[es]$')
      call mkdir(iconv(a:dir, &encoding, &termencoding), 'p')
    endif
  endfunction
augroup END

This has the benefit of prompting of me when I try to save a file in non-existent directory. However, I feel like the less configuration I have to rely on the better, and also, :!mkdir %:p:% feels, and looks much more badass.


Follow me on Twitter to keep up with what I’ve learned building my personal finance tool, Stash.

Subscribe to my mailing list to learn more about technology and business.

Your email will never be shared with anyone else.

Read more posts …
Written: November 28th, 2011
Categories: vim, automation