If I use:
:tabedit file1 file2
I get:
E172: Only one file name allowed
Is there any way to use :tabedit
with multiple file names? Or another way to open multiple tabs at once?
As far as I know, the only built-in way to do this is:
:args *.vim
:tab all
First, the :args
will replace the argument list. The argument list lists the files you opened Vim with; so vim file1 file2
means that the argument list contains file1
and file2
.
We can modify this at runtime, and Vim will open a buffer for every new entry in the argument list.
See :help argument-list
for more information.
The :all
command opens a window for every entry in the argument list, the :tab
command executes a command, and opens a new tab when the command given would open a new window.
Caveats
There are some caveats to this method.
First of all, there is no check for duplicates, so you can end up with 2 or more tabs for the same buffer.
But the largest problem is that it replaces all your tabs with what is in the argument list; so you lost all existing tabs.
You can slightly circumvent this by using :argadd *.vim
to add to the argument list, instead of replacing it; but commands such as :edit
or :tabedit
do not alter the argument list, and you will lose those tabs unless you add them to the argument list (you still have them in the buffer list, though). You may also not want to open everything in your argument list in a tab, perhaps you just want to open 2 files as an additional tab.
You could perhaps make this slightly better by first adding adding all currently open tabs to the argument list (which I can't really get to work), but this still is far from perfect. If a tab has 2 or more windows, it will still modify them...
:argdo tabedit %
, which opens a new tab for each argument. Starting with one tab and opening n new ones would naturally leave you with n+1 tabs. :tab all
is a much better solution.
argadd * | tab all
the best and am surprised nobody else mentioned it, even across several SE sites with the same question.
Commented
Feb 21, 2020 at 18:31
Given the problems & complexity in my other answer using the "built-in" way by modifying the argument list, I've added by own small function to do this:
" Open multiple tabs at once
fun! OpenMultipleTabs(pattern_list)
for p in a:pattern_list
for c in glob(l:p, 0, 1)
execute 'tabedit ' . l:c
endfor
endfor
endfun
command! -bar -bang -nargs=+ -complete=file Tabedit call OpenMultipleTabs([<f-args>])
You can now use :Tabedit *.vim
. This function will expand all globbing patterns, and execute :tabedit <f>
for every file. You can add as many pathnames as you want, for example this all works:
:Tabedit file.rb
:Tabedit *.c
:Tabedit file1.py file2.py _*.py
:Tabedit /etc/hosts file{1,2}.sh
Well, and so forth...
I put this in a little globedit.vim plugin, which also contains command for :Edit
, :Split
, etc.
[<f-args>]
instead of "<args>"
-- that way, the pattern
parameter will be a List (array) and you won't have to deal with manually splitting the string. (2) Pass a non-zero as the third argument to glob()
to have it return a List. Then you can avoid manually splitting there, too. You'll need to pass 0
as the second argument to skip it without affecting the behavior: glob(l:p, 0, 1)
.
l:p
in the first for loop? Without l:
, the line for c in glob(l:p, 0, 1)
will error out.
l:
is implied, so it should work with and without.
Commented
Oct 21, 2020 at 13:09
You can use :next
command instead, in example:
:n file1 file2
or:
:n *.txt
:args *.vim
? At least, it behaves the same, and :help next_f
documents it as being the same...
Commented
Feb 23, 2015 at 15:56
From command line you can use:
vim file1 ... filen -p
And if you forgot to use -p
still you can use
:tab ball
Btw, don't forget that the variable tabpagemax
(default value 10) limits how many tabs you can open this way.
tabpagemax
that the remaining files get opened in the last tab but open the files in horizontal split
within that last tab.
You actually can open new tabs and keep your current tabs without writing new functions (see this answer on Stack Overflow):
:args file1 file2 | argdo tabe
You may want to open a new empty tab (:tabe
) before doing that, because the first file will open in the current tab. Also, an extra empty tab will be left open (see :help argdo
to understand why).
:tabnew | args file1 file2 | argdo tabe | tabdo syntax on
The you need press Enter to clear the prompt and type :close
to remove the last (empty) tab ... it's an awful lot of typing, and only worth it if you're opening a lot of tabs (for 2 or 3 files, just using :tabe
twice would be faster).
Commented
Mar 26, 2015 at 16:04
:args *.c | tab all
. The other question is more popular than this, so it's good to link it as well.