121

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
  • 2,984
  • 4
  • 27
  • 30
  • 18
    Thanks for mentioning the -p option; that's what I was looking for – user72923 Oct 21 '13 at 21:40
  • 6
    I 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 :-) – gen_Eric Oct 15 '14 at 20:32
  • 2
    Related: [How can I open multiple tabs at once?](http://vi.stackexchange.com/questions/2108/how-can-i-open-multiple-tabs-at-once) at Vim SE – kenorb Feb 23 '15 at 15:38
  • Related: [Open several files in new tabs with VIM](http://stackoverflow.com/questions/11430361/open-several-files-in-new-tabs-with-vim) at Stack Overflow. – Mihai Capotă Mar 26 '15 at 15:40
  • Related: [How to open many tabs for many files in vim?](http://superuser.com/questions/378429/vim-open-many-tabs-for-many-files) at Super User. – Mateusz Piotrowski Jan 16 '16 at 22:48

4 Answers4

112
: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
  • 34,610
  • 8
  • 97
  • 89
  • 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 Aug 04 '10 at 09:52
  • I edited the answer to clarify what I meant by "arguments". I meant Vim's argument list rather than arguments to `:tab all`. – garyjohn Aug 04 '10 at 15:39
  • 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 Aug 05 '10 at 00:05
  • 1
    I 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 Aug 05 '10 at 00:31
  • 10
    The 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 Jul 19 '12 at 23:36
  • 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. – Rey Abolofia May 13 '13 at 17:32
  • 5
    If a tab is already open, substitute `argadd` for `args`. – cdosborn Jun 16 '15 at 19:17
  • 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 Feb 10 '16 at 23:20
  • Does this work on neovim? In my case this only works on vim... – Blaszard Aug 08 '18 at 14:04
11

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ă
  • 1,143
  • 10
  • 12
  • If you're adding new tabs to an existing set, wouldn't it be easier just to `:tabe file1`? – jpaugh Sep 10 '19 at 14:29
  • @jpaugh, the question is about multiple files. – Mihai Capotă Sep 10 '19 at 18:05
  • Wouldn't `:args | argdo tabe` answer the question, though? What's the reason for adding new files this way? – jpaugh Sep 10 '19 at 20:34
  • @jpaugh, this is what the question asks for: "I believe the vim -p option will open in tabs, but I'm hoping to find a way to do it when vim is already open." – Mihai Capotă Sep 11 '19 at 22:57
  • This did what I needed! I used a wildcard `:args *.c | argdo tabe` With the top answer it was opening some files in windows and some in tabs, but this opens all of them in tabs. – mindthief Sep 09 '21 at 23:27
3

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
  • 738
  • 2
  • 11
  • 26
1

I mapped @Mihai Capotă soltuion, adding an automatic change directory to current file. Add this line to $HOME/.vimrc file:

nnoremap <C-S-t> :cd<Space>%:p:h<CR>:tabe<CR>:args<Space>*.html<Space>\|<Space>argdo<Space>tabe<Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left>

Code explanation:

<C-S-t>

map to Crtrl-Shit-T (like FireFox is Ctrl-T for new tab)

:cd<Space>%:p:h<CR>

change directory to the one in current file open in VIM, read: https://vim.fandom.com/wiki/Set_working_directory_to_the_current_file

:tabe<CR>

read @Mihai Capotă soltuion

:args<Space>*.html<Space>\|<Space>argdo<Space>tabe

read same

<Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left>

move cursor to asterisk

To improve:

  1. the current tap will be a blank (just close it Ctrl-W-q or :q). Ideally this tab should be autoclosed (also from buffer).
  2. go to previous tap may be easy if you remember it, e.g. if it was 1st tab then just press 1gt or :tabr. Ideally this go to previous should be automatic.

PD: it might be a comment, not a solution, if so, then please comment it, I cannot.

Xopi García
  • 111
  • 2