我已经需要这几次了,只是现在它发生在我身上,也许Vim可以为我做这件事.我经常保存数量很多的文件,其名称无关紧要(无论如何它们都是临时的).
我有一个充满文件的目录:file001.txt,file002.txt ...(它们实际上并没有命名为"filexxx.txt" - 但为了讨论......).我经常保存一个新的,并命名为file434.txt.既然这是我经常做的事情,我想跳过命名检查部分.
是否有一种方法可以制作vim脚本来检查目录中的最后一个filexxx.txt,并将当前缓冲区保存为filexxx + 1.我应该怎么写这样的东西?以前有人做过这样的事吗?
所有建议都表示赞赏.
ram*_*ion 10
把以下内容放入 ~/.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
"
所以,如果你在一个目录temp0000.txt通过temp0100.txt都已经用完了,你这样做:WriteNextUnused temp%04d.txt,它会返回当前缓冲区temp0101.txt.