How to write sequential named files in Vim?

I needed this several times, and only now it happened to me that maybe Wim could do it for me. I often save files, the number of which is many, and whose names do not matter (they are still temporary).

I have a directory full of files: file001.txt, file002.txt ... (they are not really called "filexxx.txt" - but for the sake of discussion ...). I often save a new one and name it, for example, file434.txt. Now, since I do this often, I would skip the name verification part.

Is it possible to make a vim script to check the last file xxx.txt in the directory and save the current buffer as filexxx + 1. How should I write something like this? Has anyone done something like this before?

All tips were appreciated.

+3
source share
3 answers

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
"

, , temp0000.txt temp0100.txt :WriteNextUnused temp%04d.txt, temp0101.txt.

+10

script, ? python script, , . script "high.py" - . VIM

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

, . 0 .

#!/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
+1

You can write scripts for vim in many powerful languages ​​(depending on how your vim is compiled), for example perl, python, ruby. If you manage to use vim compiled with the appropriate interpreter for one of these languages, this will probably be the easiest way to write the script you want.

0
source

Source: https://habr.com/ru/post/1707509/


All Articles