Open several files in new tabs with VIM - vim

How can I open several files using wildcards in a new tab for each file with VIM?
Similar to How can I open several files at once in Vim? but in new tabs instead of buffers.

Use the -p flag:
vim -p *.c
The above will open all files matching *.c.
You can also create an alias for this (I have alias vip='vim -p' and I only type vip *.c for the above example)

If you are in Vim, this command will open each html file in the current directory in its own tab and restore syntax support (disabled by :argdo):
:args *.html | argdo tabe | tabdo syntax on
If you are in your shell, go for the other answers.

If you want to open only .md.
vim -p *.md
Or after opening vim, use :args to specify md files.
:args *.md
:tab all

with the p option: vim -p file1.cc file2.cc file3.cc

To open files in new tabs without replacing the arguments or tabs that are already open:
:argadd *.c | tab all
Also, to search for files in subdirectories:
:argadd code/**/*.c | tab all

Related

How can I open file with modifiable on

I want to open file with modifiable buffer set noma ON by default for this specific file.
for example something like this:
vi file1.txt noma
vi file2.txt ma , # default
OR at least inside vim:
:tabnew file1.txt noma
Thanks
You can add that to your .vimrc, which will set the flag if the file has that name:
augroup ReadOnly
autocmd!
autocmd BufReadPost file1.txt set noma
augroup END
That will set it both if the file exists (BufReadPost).
Edit: The view command allows you to open a file in readonly mode.
For reference: :help autocmd-groups and :help autocmd-define
You can use the -R command-line option to open a file in 'readonly' mode (which includes 'nomodifiable' and also other options that are useful when viewing files.)
$ vim -R file1.txt
The view command is usually available as a shortcut to vim -R, so this is often also possible:
$ view file1.txt
If you already have a Vim running and want to use it to open a file in read-only mode in a new tab, you can use this sequence:
:tab sview file1.txt
The :view command opens a file in read-only mode, the :sview command does so in a split and the :tab modifier has Vim do so in a new tab instead.

Is there a way for vim to open all .C and .H files in a directory and all its subdirectories?

I want to execute a substitution in vim on around 20 *.C and *.H files, and I want to open them all at once. All files are distributed in multiple nested directories, and the command is executed in the top parent directory. Can I do this using only vim, or do I need the "find" command and the "-p" option as shown on this answer?
Start Vim at the root of your project:
$ vim /path/to/project
Add the relevant files to the arglist, recursively:
:argadd **/*.C **/*.H
See :h starstar and :h :argadd.
Perform your substitution on every file in the arglist:
:argdo s/foo/bar/gc
See :h argdo and :h s_flags.

Is there a way to open multiple files at once from vim?

When launching vim from the command line, I can do for example vim *.txt to open all text files in a directory at once.
For some reason, trying the same from inside vim ( :e *.txt ) gives an error: E77: Too many file names.
Is there a reason why vim refuses to open multiple at once? Is there a way to change that?
This should work :
:next *.txt
It's done in two operations.
Open all *.js files in as many vertical splits:
:argadd *.js
:argdo vs
in horizontal splits:
:argdo sp
in tabs:
:argdo tabe
:args *.txt also works.
If it helps there is more information on this topic at :help argument-list
and :help 07.2. Both of those sections help explain how to use the argument list and how the buffer list is not the same thing.
Also, to add to the other answers, when you first start vim you can open multiple files at the same time, e.g.:
vim *.txt

How do I make vim open all files matching a pattern in different tabs?

In a given working directory, if I do
:tabe **/test*.py
vim complains with E77: Too many file names. What if I want it to open every matching file in a separate tab? There must be a way to do it, but I can't find it.
You could use the args list and argdo like so:
:args **/test*.py
:argdo tabe %
However, the syntax event is turned off by argdo (to speed up the normal use case), so the files will be loaded without syntax at first. You could follow it up with a :syntax on to force the syntax event on all loaded buffers. Compressed into one line (need to wrap argdo in execute so it doesn't absorb the following |):
:args **/test*.py | execute 'argdo tabe %' | syntax on
Alternately, you can open vim from the command line via:
vim -p **/test*.py
But that will max out at 10 tabs.
You can use the following:
:next **/test*.py
It opens all the files.
To map it
nmap <c-d> :args **/*.tpl<bar>execute 'argdo tabe %'<bar>syntax on<cr>
But still it displays list of files, you have to press enter few times (depending of number of files).
This functionality can be included as a command in your .vimrc file:
"open all files in seperate tabs
command -nargs=1 OpenAll call <SID>openAll(<f-args>)
function! s:openAll(dir)
execute 'args ' . a:dir
silent argdo tabe %
syntax on
endfunction
With this function running :OpenAll **/*.py from vim will quickly open all files into new tabs
None of the other answers works for me, but this is fine:
find <path> -iname <pattrn> | xargs -o vim -p
all files are visible in different tabs
file lookup is recursive
Note, vim can limit tabs - to be changed by set tabpagemax=42.
Also, if you wonder how to close all tabs at once, use :qa

Open all files that match a certain pattern in Vim

I’m in ~/src. I can do git grep _pattern_ and get a list of all *.cpp or *.hpp files that match this pattern.
Now I would like to go through all the files that match the pattern and make edits on them. How do I do this in Vim? (Basically, I want Vim to go through my directory like git grep does, and jump me to the right files.)
You can use the single inverted commas (also a unix shell feature), something like:
vim `git grep --name-only <your expression>`
In bash, you could do
vim $(grep -l _pattern_ *.cpp *.hpp)
but that's a bash feature, not a vim feature.
you can use the args ex command:
:args *.cpp *.hpp
This will open all cpp and hpp files in the current directory.
You can use any file path expansions available to :grep as well.
You could possibly set the grepprg and grepformat options to run git grep... and interpret the result. This would then let you run the command :grep and read the results into the quickfix buffer - see :h quickfix for more information. You can then step through them with :cnext and :cprev, or :copen to open a separate window with the list of files - putting the cursor on a filename and pressing return will open that file for editing.
The advantage of this over Zoran's and ammoQ's suggestions is that it will not read the files into memory until you want to edit them. Their suggestion will load possibly hundreds of files into memory at once, and can be a nightmare to manage. It is also cross platform so should work on Windows without having to use a third-party shell such as cygwin bash.
By properly using the quickfix list, you can even go immediately to the the right line (using the :help quickfix commands, eg. :cn or :cw). So, if you are using bash or zsh:
vim -q &lt(git grep foo)

Resources