2

I would like to open, say, 4 files and then have windows arranged in a grid so that the first file was in top left, second bottom left, third top right and fourth bottom right.

So a command like this (with an imaginary argument -Q)

vim -Q <some stuff> file1.txt file2.txt file3.txt file4.txt

would result in

------------------------------
| file 1     |       file 3   |
|            |                |
|----------------------------- 
| file 2     |       file 4   |
|            |                |
|----------------------------- 

You can use the -o or the -O arguments to produce a window for each file but the resulting windows are split, respectively, horizontally or vertically but not in a grid as shown above.

I've noticed that there is a -c argument which allows a command to be executed after loading the first file but I'm not sure what sort of command I could issue to define window layout and in any case any command would probably need to be executed after the last file had been loaded.

Would be interested in any suggestions.

BTW although my example uses four files I would like a more general solution as I'm more usually dealing with 8 or 10 files.

Thanks

glaucon
  • 121
  • 2
  • Can't you simply open all the files, setup your windows as you want, create a sessions with :mksession ~/.my-important-session and then start vim with vim ~/.my-important-session? You could even create a bash alias for this command. EDIT Oh or maybe I misunderstood and what you actually want is to keep the layout but being able to use it with different files each time? – statox Sep 02 '20 at 09:08
  • @statox thanks for your reply. A session file won't really work because it's a different set of N files each time. I did consider writing a script to write a session file so when I wanted to edit a given set of files I would run the script and it would produce a custom session file and then launch vim however having inspected the interior of a session file it's a little off-putting and I hope there might be a simpler approach than that. If there was always going to be, say, 4 files I probably would do something like the script approach but with a variable number of files, not so keen. – glaucon Sep 02 '20 at 09:15
  • I think I wouldn't mess with the sessions files, rather I would get inspiration from this plugin code which creates the splits depending on the number of files. This is probably the easiest way to have a reproducible layout and you can add the logic you want. – statox Sep 02 '20 at 09:41
  • There's :h winlayout() and :h winrestcmd() if you really want to mess with it. – Matt Sep 02 '20 at 09:49
  • 1
    Welcome to [vi.se]! I believe this is a duplicate of https://vi.stackexchange.com/q/26099/18609, in fact the answers to that question cover both the case of using -c or a custom session file to create a specific layout on startup. – filbranden Sep 02 '20 at 11:31
  • 1
    Agreed with @filbranden , in this case, you may want to use argc and argv or arglist to decide what files to open and split where, instead of that question hard-coding the filenames. – D. Ben Knoble Sep 02 '20 at 13:00

0 Answers0