Re: Opening files in a certain pattern

10 views
Skip to first unread message

Manas

unread,
Jul 27, 2020, 5:26:28 PM7/27/20
to v...@vim.org
Hi, I have 2 directories, supposedly dir1 and dir2. And both of them
contains equal number of files (say n each) which are named in the
following fashion:
- In dir1: All files are named as L1.md, L2.md, ...., Ln.md
- In dir2: All files are named as A1.md, A2.md, ...., An.md

How can I open all of those files such that each window contains two
vertically-split panes namely, Lx.md and Ax.md (where x is any number)
and they open in n-different windows?

First I was thinking of writing a bash script to open but I got stuck at
the initial step of finding the appropriate Vim command to open two
files in a new tab and in vertically-split panes from Ex mode (as I would
have passed this command using -c flag from my script).

Thanks
--
Manas
CSAM Undergraduate | 2022
IIIT-Delhi, India

Gary Johnson

unread,
Jul 27, 2020, 6:10:27 PM7/27/20
to vim...@googlegroups.com
On 2020-07-28, Manas wrote:
> Hi, I have 2 directories, supposedly dir1 and dir2. And both of them
> contains equal number of files (say n each) which are named in the
> following fashion:
> - In dir1: All files are named as L1.md, L2.md, ...., Ln.md
> - In dir2: All files are named as A1.md, A2.md, ...., An.md
>
> How can I open all of those files such that each window contains two
> vertically-split panes namely, Lx.md and Ax.md (where x is any number)
> and they open in n-different windows?

How's this for a start? The number n here is fixed, but you could
use a while loop instead of the for loop, increment n in each
iteration, and break when one of the files doesn't exist. See
":help filereadable()".

let n = 4
for i in range(n)
exe "tabnew" printf("dir2/A%d.md", i)
exe "vnew" printf("dir1/L%d.md", i)
endfor

> First I was thinking of writing a bash script to open but I got stuck at
> the initial step of finding the appropriate Vim command to open two
> files in a new tab and in vertically-split panes from Ex mode (as I would
> have passed this command using -c flag from my script).

vim -c "tabnew dir2/A${n}.md" -c "vnew dir1/L${n}.md"

That's the general idea, but probably not what you want finally as
it opens only one vim instance and only one pair of files.

Regards,
Gary

Manas

unread,
Jul 27, 2020, 6:57:58 PM7/27/20
to vim...@googlegroups.com
On Mon, Jul 27, 2020 at 03:08:49PM -0700, Gary Johnson wrote:
> How's this for a start? The number n here is fixed, but you could
> use a while loop instead of the for loop, increment n in each
> iteration, and break when one of the files doesn't exist. See
> ":help filereadable()".
>
> let n = 4
> for i in range(n)
> exe "tabnew" printf("dir2/A%d.md", i)
> exe "vnew" printf("dir1/L%d.md", i)
> endfor
>
That is the idea which I was thinking but opening other windows (after
the first one) is giving me troubles.

> > First I was thinking of writing a bash script to open but I got stuck at
> > the initial step of finding the appropriate Vim command to open two
> > files in a new tab and in vertically-split panes from Ex mode (as I would
> > have passed this command using -c flag from my script).
>
> vim -c "tabnew dir2/A${n}.md" -c "vnew dir1/L${n}.md"
>
> That's the general idea, but probably not what you want finally as
> it opens only one vim instance and only one pair of files.
>
That's right. Also the above command actually opens an empty buffer also.

Gary Johnson

unread,
Jul 27, 2020, 7:20:44 PM7/27/20
to vim...@googlegroups.com
On 2020-07-28, Manas wrote:
> On Mon, Jul 27, 2020 at 03:08:49PM -0700, Gary Johnson wrote:
> > How's this for a start? The number n here is fixed, but you could
> > use a while loop instead of the for loop, increment n in each
> > iteration, and break when one of the files doesn't exist. See
> > ":help filereadable()".
> >
> > let n = 4
> > for i in range(n)
> > exe "tabnew" printf("dir2/A%d.md", i)
> > exe "vnew" printf("dir1/L%d.md", i)
> > endfor
> >
> That is the idea which I was thinking but opening other windows (after
> the first one) is giving me troubles.

If it's still giving you troubles, you could post here what you have
and we could perhaps find the problem. If you do that, please be
specific about what you mean by giving you troubles.

Regards,
Gary

Tim Chase

unread,
Jul 27, 2020, 7:32:16 PM7/27/20
to Manas, vim...@googlegroups.com, v...@vim.org
You might try something like this:

$ vim -p -c 'set tabpagemax=50' -c 'tabdo exec "vsp ".substitute(substitute(expand("%"),"dir1", "dir2", ""), "/L", "/A", "")' dir1/*

(there's a space after the "vsp" in case mail between here and there
decides to eat it)

By default 'tabpagemax' is 10, so you don't need to change that to
50 if you have 10 or fewer files. Just set it to the number of files
you have. The "-p" then opens each dir1/L*.md file in its own tab
(they're specified on the command-line). It then iterates over each
of those tabs (tabdo), transforms the current filename into its mate's
filename (s/dir1/dir2/ and s@/L@/A@) and builds an command to
vertically split (vsp) as a string, and then :exec's it to do the
actual vertical split in each tab.

-tim




Manas

unread,
Jul 27, 2020, 7:33:47 PM7/27/20
to vim...@googlegroups.com
I found `:tabl` which will take me to last tab. So I come up with
opening first 2 panes in starting window as:

$ vim -O2 dir1/L1.md dir2/A1.md

And for opening next 2 panes in new tab after the first tab, first I
opened one file using tab sp and then going to that tab using tabl,
and then vertical splitting second file from dir2. The final command
looked like this:

$ vim -O2 dir1/L1.md dir2/A1.md -c "tab sp dir1/L2.md" -c "tabl" -c "vsp
dir2/A2.md"

It works for me. And I can generate that command in shell script.

Can there be some improvement upon this?

Manas

unread,
Jul 27, 2020, 7:42:13 PM7/27/20
to Tim Chase, v...@vim.org
On Mon, Jul 27, 2020 at 06:32:01PM -0500, Tim Chase wrote:
> You might try something like this:
>
> $ vim -p -c 'set tabpagemax=50' -c 'tabdo exec "vsp ".substitute(substitute(expand("%"),"dir1", "dir2", ""), "/L", "/A", "")' dir1/*
>
> (there's a space after the "vsp" in case mail between here and there
> decides to eat it)
>
> By default 'tabpagemax' is 10, so you don't need to change that to
> 50 if you have 10 or fewer files. Just set it to the number of files
> you have. The "-p" then opens each dir1/L*.md file in its own tab
> (they're specified on the command-line). It then iterates over each
> of those tabs (tabdo), transforms the current filename into its mate's
> filename (s/dir1/dir2/ and s@/L@/A@) and builds an command to
> vertically split (vsp) as a string, and then :exec's it to do the
> actual vertical split in each tab.
>
> -tim
>
I am sorry, I missed your mail by a minute. That command works
beautifully. I tried it on my directory structure and works.

Thanks everyone for help!

Tim Chase

unread,
Jul 27, 2020, 7:50:13 PM7/27/20
to vim...@googlegroups.com
On 2020-07-27 18:32, Tim Chase wrote:
> You might try something like this:
>
> $ vim -p -c 'set tabpagemax=50' -c 'tabdo exec "vsp
> ".substitute(substitute(expand("%"),"dir1", "dir2", ""), "/L",
> "/A", "")' dir1/*

Apparently the "tabpagemax" doesn't get set until after Vim has
opened the args in each tab, so this doesn't work for n>10. So you
have to do the splitting after changing 'tabpagemax'. I had better
luck with this

$ vim -c 'set tabpagemax=50' -c 'args dir1/*' -c 'argdo tabe %' -c 'tabdo exec "vsp ".substitute(substitute(expand("%"),"dir1", "dir2", ""), "/L", "/A", "")'

It does open one extra window, but seems to work better than the
previous one.

-tim




Manas

unread,
Jul 27, 2020, 8:12:47 PM7/27/20
to vim...@googlegroups.com
Can we use `--cmd` instead of `-c` as according to manpage:
--cmd Like using "-c", but the command is executed just before processing any vimrc file.
Reply all
Reply to author
Forward
0 new messages