124

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
  • 3,064
  • 4
  • 30
  • 31

4 Answers4

120
: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
  • 36,494
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).

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