I’ve needed this a few times, and only now it occured to me, that maybe Vim could do it for me. I often save files whose numbers are many, and whose names do not matter (they’re temporary anyway).
I have a directory full of files: file001.txt, file002.txt …(they’re not really named “filexxx.txt” – but for the sake of discussion …). I often save a new one, and name it for example, file434.txt. Now since that’s something I do often, I’d like to skip the naming checking part.
Is there a way vim script can be made as to check for the last filexxx.txt in the directory, and save the current buffer as filexxx+1. How should I go about writing something like that ? Has anyone done something like this before ?
All advices appreciated.
Put the following in
~/.vim/plugin/nextunused.vim" nextunused.vim " find the next unused filename that matches the given pattern " counting up from 0. The pattern is used by printf(), so use %d for " an integer and %03d for an integer left padded with zeroes of length 3. function! GetNextUnused( pattern ) let i = 0 while filereadable(printf(a:pattern,i)) let i += 1 endwhile return printf(a:pattern,i) endfunction " edit the next unused filename that matches the given pattern command! -nargs=1 EditNextUnused :execute ':e ' . GetNextUnused('<args>') " write the current buffer to the next unused filename that matches the given pattern command! -nargs=1 WriteNextUnused :execute ':w ' . GetNextUnused('<args>') " To use, try " :EditNextUnused temp%d.txt " " or " " :WriteNextUnused path/to/file%03d.extension "So if you’re in a directory where
temp0000.txtthroughtemp0100.txtare all already usedand you do
:WriteNextUnused temp%04d.txt, it will write the current buffer totemp0101.txt.