1

I have a list of files like step-1.py, step-2.py etc and if I try to open them with vim files-*.py then I get a list of buffers like:

:ls
  1 %a   "step-10.py"
  2      "step-11.py"
  3      "step-12.py"
  4      "step-13.py"
  5      "step-14.py"
  6      "step-15.py"
  7      "step-16.py"
  8      "step-17.py"
  9      "step-18.py"
 10      "step-19.py"
 11      "step-1.py" 
 12      "step-2.py" 
 13      "step-3.py" 
 14      "step-4.py" 
 15      "step-5.py" 
 16      "step-6.py" 
 17      "step-7.py" 
 18      "step-8.py" 
 19      "step-9.py"

but I want the files to be loaded in order so that the buffer number corresponds to the step number (:b6 should load a buffer with step-6.py file in it).

So I've been trying to use craft the list in the proper order and supply it to vim... like...

ls step-* | sort -n -t- -k2 | vim -

...but that just loads a single unnamed buffer with the contents step-1.py step-2.py step-3.py step-4.py step-5.py step-6.py step-7.py step-8.py step-9.py step-10.py step-11.py step-12.py step-13.py step-14.py step-15.py step-16.py step-17.py step-18.py step-19.py .

If I instead try that like files=$(ls step-* | sort -n -t- -k2); vim $files then that still just gives me an empty buffer like:

:ls
  1 %a   "step-1.py step-2.py step-3.py step-4.py step-5.py step-6.py step-7.py step-8.py step-9.py step-10.py step-11.py step-12.py step-13.py step-14.py step-15.py step-16.py step-17.py step-18.py step-19.py " line 1

How do I achieve this?

alec
  • 795
  • 6
  • 20
  • 1
    Is it an option to rename the first 9 files with a leading zero? e.g. step-01.py That would solve the problem. – Heptite Aug 15 '21 at 00:36

2 Answers2

3

That should be enough:

ls -v *.py | xargs gvim
Matt
  • 20,685
  • 1
  • 11
  • 24
0

Cleanest way: vi $(ls step-* | sort -n -t- -k2).


In trying the approach described in the question, vim needs the filenames provided to it as an array. So take files=$(ls step-* | sort -n -t- -k2); vim $files and merely add ( and ) around the existing variable definition so that it looks like...

files=($(ls step-* | sort -n -t- -k2)); vim $files and vim will open that list of files in their respective buffers in the proper order:

:ls
  1 %a   "step-1.py"                    line 9
  2      "step-2.py"                    line 0
  3      "step-3.py"                    line 0
  4      "step-4.py"                    line 0
  5      "step-5.py"                    line 0
  6      "step-6.py"                    line 0
  7      "step-7.py"                    line 0
  8      "step-8.py"                    line 0
  9      "step-9.py"                    line 0
 10      "step-10.py"                   line 0
 11      "step-11.py"                   line 0
 12      "step-12.py"                   line 0
 13      "step-13.py"                   line 0
 14      "step-14.py"                   line 0
 15      "step-15.py"                   line 0
 16      "step-16.py"                   line 0
 17      "step-17.py"                   line 0
 18      "step-18.py"                   line 0
 19      "step-19.py"                   line 0
alec
  • 795
  • 6
  • 20