I use buffers as "tabs" in Vim, and open new files using :e[dit] command. Why can't I use globbing with :e command?
:e some_dir/*
E77: Too many file names
Just use :n instead:
:n some_dir/*
You can use globbing with :edit and other commands; however, there must be a single, unique result of the glob, because the :edit command only takes a single file. (How else should it display multiple matches in the single current window?)
If you want to edit multiple files sequentially, you can use :args or :argadd instead. Note that the :split command does not take multiple files, neither (probably because of the risk that many matches will inadvertently cause an impractically large number of window splits), but you could write your own :Split command that provides this functionality.
You should use :args or :argadd instead.
:args src/*
:tab all
First command loads all files from src/ folder to buffers and second command puts each buffer then into separate tabs.
:help arglist will give more information
Related
Consider the following directory tree:
root/include/file.hpp
root/source/file.cpp
root/images/file.png
The command line is inside the directory root.
In the vimrc file, there is set wildignore=*.png.
If you open Vim in the folder root and run :next */file.*, it opens only file.hpp and file.cpp.
However, if you launch Vim from command line with vim */file.*, it opens all three files.
So, when feeding it a filename, it first loads the files, then vimrc? Is there a way to ignore extensions when opening files with Vim through the command line? Or to make Vim load vimrc first?
In the first scenario, the glob expansion is done by Vim and thus obeys the rules in your vimrc.
In the second scenario, the glob expansion is done by your shell and there's no reason to expect it to obey the rules in your vimrc.
You can do something like $ vim -c "next */file.*", which essentially opens Vim without a filename and executes next */file.*.
Or you can exclude the pattern directly in your shell. Assuming you have extglob set, this can be done in bash with $ vim !(file.png).
When doing :next */file.* from within Vim, vim expands the wildcard and filters by wildignore. When doing vim */file.* from your shell, the shell expands the wildcard, and passes all three files to Vim.
Depending on your shell, this will probably work instead:
vim +"args */file.*"
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
Is it possible to have ctags generate tags for filenames as well? I would like to be able to jump to a file given a filename. :find seems to be awfully slow compare to tags...
Try running ctags -R --extra=f .
The --extra=f option tells ctags to:
"Include an entry for the base file name of every source file (e.g. "example.c"), which addresses the first line of the file."
When you open vim, you can the use :tag <filename> to jump to the first line of the file.
You can open the filename under the cursor with gf
http://vim.wikia.com/wiki/Open_file_under_cursor
You can also use cscope:
:cs find f <filename>
or, if you've setup cscope in vim as recommended in :help cscope, put the cursor on top of a filename, and press <C-_>f.
Have you tried setting your path and then using vim's 'gf' command?
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
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 <(git grep foo)