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
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
.
: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.)
Commented
Apr 26, 2020 at 5:23
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.
ba
seems to be what you're looking for.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.