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
  • Have you checked this answer? ba seems to be what you're looking for. – Biggybi Apr 26 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. – Smie Apr 26 at 12:05
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?

| improve this answer | |
  • 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 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. – Martin Tournoij Apr 26 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.

| improve this answer | |
0

This is pretty straightforward, but the caveat is that it will add duplicates to your arglist.

You could add the files you need to your argument list with argadd (:h argadd) and open them in a split with ba (:h ba).

For example, to open every file named file[something] in vertical splits:

:argadd file* | vert ba
| improve this answer | |

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.