tags:

views:

161

answers:

3

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.

A: 

You can write scripts for vim in many powerful languages (depending on how your vim is compiled), such as perl, python, ruby. If it's possible for you to use a vim that's compiled with the appropriate interpreter for one of these languages, this would probably be the easiest way for you to write the script you desire.

Alex Martelli
+10  A: 

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.txt through temp0100.txt are all already used and you do :WriteNextUnused temp%04d.txt, it will write the current buffer to temp0101.txt.

rampion
+1 for pure vim and noting requiring python
ojblass
Exactly what I was looking for!
ldigas
+1  A: 

How about a script that you can shell out to? Here is a quick python script that should accomplish what you need. Save the script as "highest.py" to somewhere in your path. From VIM, get into command mode and type

:!python highest.py "file*.txt"

It returns the highest numbered file in the current directory, or a message that no files matched. It handles leading 0's and could be generalized for more complex patterns is need be.

#!/usr/bin/python
#
# Finds the highest numbered file in a directory that matches a given pattern
# Patterns are specified with a *, where the * will be where the number will occur.
#

import os
import re
import sys

highest = "";
highestGroup = -1;

if (len(sys.argv) != 2):
        print "Usage: python high.py \"pattern*.txt\""
        exit()

pattern = sys.argv[1].replace('*', '(\d*)')

exp = re.compile(pattern)

dirList=os.listdir(".")

for fname in dirList:
        matched = re.match(exp, fname)
        if matched:
                if ((highest == "") or (int(matched.group(1)) > highestGroup)):
                        highest = fname
                        highestGroup = int(matched.group(1))

if (highest == ""):
        print "No files match the pattern: ", pattern
else:
        print highest
mmorrisson