How to get vim to open multiple files into tabs at once

91

23

Is it possible to get vim to open multiple files into tabs, similar to the way the args <path> command will open multiple files into buffers?

Doing something like :tabe ./* results in the error "E77: Too many file names", even though the number of files is less than the value set in the tabpagemax property.

(I believe the vim -p <files> option will open in tabs, but I'm hoping to find a way to do it when vim is already open.)

Ash

Posted 2010-08-04T06:37:59.393

Reputation: 2 574

4I found this question via Google. I was looking for how to open vim with tabs from the command line, vim -p was what I was looking for :-) – Rocket Hazmat – 2014-10-15T20:32:17.290

2

Related: How can I open multiple tabs at once? at Vim SE

– kenorb – 2015-02-23T15:38:56.797

Related: Open several files in new tabs with VIM at Stack Overflow.

– Mihai Capotă – 2015-03-26T15:40:02.407

Related: How to open many tabs for many files in vim? at Super User.

– Mateusz Piotrowski – 2016-01-16T22:48:24.033

13Thanks for mentioning the -p option; that's what I was looking for – user72923 – 2013-10-21T21:40:02.983

Answers

88

:tab all

will open all the files in Vim's argument list in individual tabs. The argument list is initially set to the list of file names given on the command line when Vim is started. The list may be changed with the :args and related commands. See

:help :all
:help argument-list

Example:

:args *.c
:tab all

will open all the .c files in the current directory in individual tabs.

garyjohn

Posted 2010-08-04T06:37:59.393

Reputation: 29 085

4If a tab is already open, substitute argadd for args. – cdosborn – 2015-06-16T19:17:12.747

This is great. It also works from the command line. If you do vim * in bash, then :tab all in vim, you can open all the contents of a directory in separate tabs. – Damien Ó Ceallaigh – 2016-02-10T23:20:16.287

I'm not sure how this is meant to work. Say if I want to open all .txt files in the current directory, what would I enter? If I enter :tab all *.txt, vim counters with "E488: Trailing characters" – Ash – 2010-08-04T09:52:43.933

I edited the answer to clarify what I meant by "arguments". I meant Vim's argument list rather than arguments to :tab all. – garyjohn – 2010-08-04T15:39:37.520

Nice, got it now. The only thing that could make this better is a one line equivalent...what do you think the chances are? – Ash – 2010-08-05T00:05:11.487

1I don't know of a single command that can do that, but you can put two commands on one line by separating them with a vertical bar, like this: :args *.c | tab all. – garyjohn – 2010-08-05T00:31:00.327

Does this work on neovim? In my case this only works on vim... – Blaszard – 2018-08-08T14:04:29.670

9The downside is that using :tab all replaces your existing tabs. Because of this, in my use case, it doesn't provide much benefit over reopening files with vim -p. However, if there were some way to stuff existing tabs into :args, it might be possible to open new tabs and keep the existing ones. – Kevin Qi – 2012-07-19T23:36:49.280

This worked great for me. I wanted to open all files in my directory with the same extension. This did the trick. Thanks so much. – Reina Abolofia – 2013-05-13T17:32:56.730

5

You actually can open new tabs and keep your current tabs without writing new functions. See this answer on Stack Overflow: https://stackoverflow.com/a/11430615/200234

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

Mihai Capotă

Posted 2010-08-04T06:37:59.393

Reputation: 793

If you're adding new tabs to an existing set, wouldn't it be easier just to :tabe file1? – jpaugh – 2019-09-10T14:29:18.020

@jpaugh, the question is about multiple files. – Mihai Capotă – 2019-09-10T18:05:45.543

Wouldn't :args | argdo tabe answer the question, though? What's the reason for adding new files this way? – jpaugh – 2019-09-10T20:34:39.787

@jpaugh, this is what the question asks for: "I believe the vim -p <files> option will open in tabs, but I'm hoping to find a way to do it when vim is already open." – Mihai Capotă – 2019-09-11T22:57:17.913

1

To open files in new tabs without replacing the arguments or tabs that are already open:

:argadd *.c | tab all

This was mentioned in a comment but I think deserves its own answer.

Also, to search for files in subdirectories:

:argadd code/**/*.c | tab all

Big McLargeHuge

Posted 2010-08-04T06:37:59.393

Reputation: 449