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?

1
  • 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, 2021 at 0:36

2 Answers 2

3

That should be enough:

ls -v *.py | xargs gvim
3
  • 1
    Historically this invocation caused vim to complain about stdin. Is that no longer the case?
    – D. Ben Knoble
    Aug 15, 2021 at 10:39
  • 2
    @D.BenKnoble It still does. Works well for Neovim or GVim.
    – Matt
    Aug 15, 2021 at 11:25
  • xargs! of course. thanks for the reminder :)
    – alec
    Aug 15, 2021 at 13:18
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
4
  • Are you using zsh?
    – D. Ben Knoble
    Aug 15, 2021 at 10:38
  • yes, using zsh.
    – alec
    Aug 15, 2021 at 12:48
  • 2
    That explains it; zsh doesn’t word-split unquoted variables like sh and bash, and it does expand arrays without extra syntax (again, unlike bash). Please include mention of zsh in the question, as the problem and solution appear very differently in different shells.
    – D. Ben Knoble
    Aug 15, 2021 at 12:56
  • oh... if I go into bash my solution doesn't seem to work there... only the first buffer is loaded.
    – alec
    Aug 15, 2021 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.