1

I can open and split multiple file like this

:split file1
:split file2
:split file3

But is there any way to open multiple files in a single command? Something like this

:split file1 file2 file3
2
  • Have you checked this answer? ba seems to be what you're looking for.
    – Biggybi
    Apr 26, 2020 at 11:45
  • @Biggybi ba opens all my buffer. But I don't want that. I want to open files may be not in the buffer or not all the buffer. Apr 26, 2020 at 12:05

3 Answers 3

3

This isn't possible with the default :split command; you'll have to use a custom command such as :Split (capital S) which loops over all the commandline arguments and calls :split:

:command! -nargs=+ -complete=file Split
            \  for s:f in [<f-args>]
            \|   exe 'split' fnameescape(s:f)
            \| endfor
            \| unlet s:f

You can improve that a bit more by calling glob(), so that :Split *.vim will work as well:

:command! -nargs=+ -complete=file Split
            \  for s:f in [<f-args>]
            \|   for s:m in glob(s:f, 0, 1)
            \|     exe 'split' fnameescape(s:m)
            \|   endfor
            \| endfor
            \| unlet s:f | unlet s:m

I wrote a little plugin a few years ago which does that for :Tabedit, :Edit, :Split, and :Vsplit.


Related: How can I open multiple tabs at once?

2
  • I tried :argdo split before you posted your answer, but the problem I had with that one is that you end up with the last file in the args list opened twice. (Not horrible, easy to drop one of the splits, but still it's not a perfect solution as is.)
    – filbranden
    Apr 26, 2020 at 5:23
  • Oh right @filbranden; I now remember that's actually one of the reasons I never use this. I'll just remove that part; someone else can provide an answer based on that with more details. Apr 26, 2020 at 5:39
2

This answer is based on Martin's solution

It add :h <mods> to let you choose the split style, and :h :command-bang to let you decide whether existing windows or tabs should be closed.

It's implemented by lambda, this lambda is composed of two execute(...) connected by .(All expressions within one level are parsed from left to right).

com -nargs=+ -bang -complete=file Split
      \ call map( [<f-args>], { i,v -> execute('<mods> split ' . fnameescape(v) )
      \ . ( i == 0 && <bang>0 &&
      \    execute('<mods>' =~# 'tab' ? 'tabonly' : 'wincmd o') ) } )

examples:

# split 3 new windows
Split a b c

# close existing windows, split 3 new right below windows
rightbelow Split! a b c

# close existing tabs, split 3 new tabs
tab Split! a b c

I didn't add the glob part, you can use :h c_CTRL-A to expand the pattern into command line directly.

2

I'd use the argument list for this.

First, populate it with the file you need with :args (:h :args). Then, use it to open its entries in a split with :all (:h all) (or :vert all for vertical splits).

:args file* | all

(file* will grab any file name starting with file.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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