14

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?

  • 1
    Related: How to get vim to open multiple files into tabs at once at superuser SE – kenorb Feb 23 '15 at 15:38
  • @kenorb Yes. And it has exactly the same answer already here, except with no explanation on what it does. So how does linking to it add any value? – Martin Tournoij Feb 23 '15 at 15:40
  • There are some other answers, and new answers can come there. Secondly there are few other similar linked/related there, so users can learn more from related onces as well. – kenorb Feb 23 '15 at 15:42
  • I've learned this one-liner: :args *.c | tab all. The other question is more popular than this, so it's good to link it as well. – kenorb Feb 23 '15 at 15:50
  • Related: Open several files in new tabs with VIM at Stack Overflow. – Mihai Capotă Mar 26 '15 at 15:49
10

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.

  • 2
    Great function and command! Some improvements: (1) Use [<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). – tommcdo Feb 23 '15 at 2:17
6

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...

  • I might be wrong, but doesn't this open an extra tab (as seen in my question about opening tabs by default)? – muru Feb 22 '15 at 23:50
  • 1
    If you're referring to my answer on that question, the difference there is that I used :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. – tommcdo Feb 23 '15 at 2:24
  • @tommcdo I was talking of Josh Petrie's answer. You wouldn't happen to one of his aliases, would you? :) – muru Feb 23 '15 at 10:41
  • Haha, nope. I must have been thinking of a different question! – tommcdo Feb 23 '15 at 10:44
2

You can use :next command instead, in example:

:n file1 file2

or:

:n *.txt
  • This is seems the same as using :args *.vim? At least, it behaves the same, and :help next_f documents it as being the same... – Martin Tournoij Feb 23 '15 at 15:56
  • It's shorter and simple and you didn't mention that. The behaviour is the same, as you asked in the question for that. – kenorb Feb 23 '15 at 15:57
2

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.

0

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).

  • You also need to fix the syntax highlighting (for some reason), as explained in the link and close the last tab, so the 'full' command would be: :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). – Martin Tournoij Mar 26 '15 at 16:04

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.