I am using :Ag in vim to search for patterns, but I can't figure out how to search and replace through ALL files in my project, or within a directly that I specify. How is this done?
In Vim, project-wide search and replace is at best a two-step process unless you install a plugin that abstracts those two steps for you like EasyGrep.
In its most basic form, project-wide search/replace looks like this:
:args `grep -nl foo *.js`
:argdo %s/foo/bar/c
It won't help you much with :Ag, though, because Vim doesn't have a built-in command similar to the :*do family that works on the quickfix list.
Drew Neil has a couple of screencasts, here and there, that deal with project-wide search/replace. The :Qfdo command mentionned at the bottom of the second post is specifically geared toward :vimgrep/:Ack/:Ag users. With that command and :Ag, the two-step process becomes:
:Ag foo
:Qfdo s/foo/bar/c
-- EDIT --
Vim now has :cdo, :cfdo, :ldo, :lfdo.
In vim, there are multiple solutions, but you have to open all the files in order to be able to do this. You can open the files in different windows, buffers or tabs, and for all the options, you have a method to operate on all files:
:windo :Ag
or
:bufdo :Ag
or
:tabdo :Ag
See :help windows, :help buffers or :help tab-page-commands for more info.
Related
There are a couple of things I do not yet understand the VIM way.
One of these is searching in a project like so (using VIM in Atom):
I use CtrlP currently for file names, but what about the contents?
How can I search with a string, and then look through a list of all occurrences using VIM and/or VIM plugins?
I've found an even better solution for this: FZF
It simply searches through everything in your project asynchronously using the :Ag command.
Use :grep or :vimgrep to search file contents. The results are put onto the "location list" which you can open by typing :cw Enter.
Syntax for :grep is, by default, the same as the grep(1) command:
:grep 'my pattern.*' /path/to/dir
By default it will search the current directory (:pwd). I added set autochdir to my .vimrc so my PWD always follows the file I'm editing.
The major difference between :grep and :vimgrep is that :vimgrep (:vim for short) uses Vim-compatible regular expressions, whereas :grep uses whatever regular expressions your &grepprg uses.
You can use a custom program by setting &grepprg to something different. I personally like ack which uses Perl-compatible regex.
Apart from fzf, there are also other excellent plugins for fuzzy finding.
telescope.nvim (neovim only): after install, just use Telescope live_grep to search through your project.
Leaderf: another fuzzy finder with good performance. After install, use Leaderf rg to search through your project.
To open a file, I highlight the row (Shift-v) in the location list and hit Enter.
I work with multiple fairly large projects with 1600+ files across 125+ directories each, but those files are interspersed with larger projects with many times that number of files and directories, which make recursive searching from the top of the project too slow and unwieldy. And I'd rather not preemptively load all those files into vim's buffers.
Fortunately, these projects have fairly good filelists that I've can parse to setup project-specific vimrc files (project.vimrc) that right now just contain a lot of "set path+=/path/to/dir" which works great for enabling stuff like "gf" to work quickly.
I'd like to find a grep solution to search for a string in the given filelist or in the directories in vim's path variable, and have it list results in the quickfix window. Is there a way to do this?
Most of the existing grep solutions seem to want you to specify the top directory and recurse, or let you grep through the files already loaded into the buffers.
If your 'path' option already contains a list of interesting directories, you can pass that on to :vimgrep, like this:
:execute 'vimgrep /pattern/' join(map(split(&path, ','), 'v:val . "/*"'))
This appends /* to each path, so that any files inside that directory are searched.
grep can take multiple directories to search recursively like via grep -r foo/ bar/ (See man grep and :h :grep for more information). However grep can be a bit slow with many files. There are some nice alternatives:
Ack is faster than regular grep because it skips VCS directories, binary files, and can search for specific type of filetypes like --perl. For Vim integration see Ack.vim
Ag the Silver Surfer is a code-searching tool similar to ack, but faster. Vim plugin: Ag.vim
git grep searches a repository very quickly. Fugitive.vim provides :Ggrep.
I recommend both Ag and git grep as they are both super fast. I have searched over 4700+ in less than a 1 second.
To test out one of these alternatives without a plugin or you simply don't want a plugin you can set your 'grepprg' option accordingly. e.g. set grepprg=ack. See :h 'grepprg' for more information.
If you really need to search a specific list of files Ack has --files-from=FILE and -x options which take a list of files in the form of file or stdin respectively.
Other tools which may be of some help for some programming languages are:
ctags builds a symbol database and allows you to jump to symbols. See :h tags-and-searches
Cscope is a tool like ctags, but think of it as ctags on steroids since it
does a lot more than what ctags provides.
locate unix tool much like find except it uses a prebuilt database. See man locate and Tim Pope's vim-eunuch.
GNU global is another symbol database that provides ctag/cscope like options as well.
As a final fallback you can use :vimgrep as it is available on all systems that Vim is available. See :h :vimg for more help.
As most of these options use the quickfix list I suggest you use some more friendly mappings to transverse the quickfix list. I use Tim Pope's unimpaired plugin which uses ]q and [q for :cnext and :cprev respectively. Some people like the quickfix to open after a search is performed then add the following to your ~/.vimrc file: autocmd QuickFixCmdPost *grep* cwindow
In conclusion you have many options however I personally doubt you can beat git grep or Ag in for general searching needs. I also use GNU Global for my symbol needs.
In Zsh, I can use filename completion with slashes to target a file deep in my source tree. For instance if I type:
vim s/w/t/u/f >TAB<
zsh replaces the pattern with:
vim src/wp-contents/themes/us/functions.php
What I'd like is to be able to target files the same way at the Vim command line, so that typing
:vi s/w/t/u/f >TAB<
will autocomplete to:
:vi src/wp-contents/themes/us/functions.php
I'm trying to parse the Vim docs for wildmode, but I don't see what settings would give me this. It's doing autocompletion for individual filenames, but not file paths. Does Vim support this natively? Or how can I customize the autocomplete algorithm for files?
Thanks for any advice!
-mykle-
I couldn't find a plugin to do this, so I wrote one. It's called vim-zsh-path-completion. It does what you're looking for, although via <C-s> rather than <Tab>. You can use it with <Tab> for even more control over what matches, though.
It's got bugs, but for basic paths without spaces/special characters, it should work. I think it's useful enough in its current state to be helpful. I hope to iron out the bugs and clean up the code, but I figured I'd start soliciting feedback now.
Thanks for the idea!
Original (wrong) answer, but with some useful information about Vim's wildmode.
Put the following in your .vimrc:
set wildmenu
set wildmode=list:longest
That will complete to the longest unique match on <Tab>, including appending a / and descending into directories where appropriate. If there are multiple matches, it will show a list of matches for what you've entered so far. Then you can type more characters and <Tab> again to complete.
I prefer the following setting, which completes to the first unique match on <Tab>, and then pops up a menu if you hit <Tab> again, which you can navigate with the arrow keys and hit enter to select from:
set wildmode=list:longest,list:full
Check out :help wildmenu and :help wildmode. You might also want to set wildignore to a list of patterns to ignore when completing. I have mine as:
set wildignore=.git,*.swp,*/tmp/*
Vim doesn't have such a feature by default. The closest buil-in feature is the wildmenu/wildmode combo but it's still very different.
A quick look at the script section of vim.org didn't return anything but I didn't look too far: you should dig further. Maybe it's there, somewhere.
Did you try Command-T, LustyExplorer, FuzzyFinder, CtrlP or one of the many similar plugins?
I use CtrlP and fuzzy matching can be done on filepath or filename. When done on filepath, I can use the keysequence below to open src/wp-contents/themes/us/functions.php (assuming functions.php is the only file under us that starts with a f):
,f " my custom mapping for the :CtrlP command
swtuf<CR>
edit
In thinking about a possible solution I'm afraid I was a little myopic. I was focused on your exact requirements but Vim has cool tricks when it comes to opening files!
The :e[dit] command accepts two types of wildcards: * is like the * you would use in your shell and ** means "any subdirectory".
So it's entirely possible to do:
:e s*/w*/t*/u*/f*<Tab>
or something like:
:e **/us/f<Tab>
or even:
:e **/fun<Tab>
Combined with the wildmode settings in Jim's answer, I think you have got a pretty powerful file navigation tool, here.
One thing that I like to do from time to time is do a quick search on the directory I am working on and find all the references to a specific string, specially when debugging code from some one else. Is it still not possible for VIM to do such search? Is there an alternative to do it directly with plain terminal?
ATM (since I'm still learning VIM) I do the search in TextMate and find the string that way.
You can use the vim command :vimgrep. This will search for a pattern in the specified files and puts the results in the quickfix window which you can then use to jump to a specific match. So for example to search for foo recursively in every .php file in your current directory you would do
:vimgrep "foo" **/*.php
Another option is the command :grep which actually calls the grep program (or whatever grepprg is set to). It also puts the results in the quickfix window for easy navigation. However this requires that you have grep on your system.
vim's an editor, not really a file searcher. It's trivially simple to call out to a shell and run 'grep', however. Assuming you're on a Unix-ish environment (TextMate - MacOs?) While in command mode, hit ctrl-z and you'll be at the shell prompt, then do
grep somestring *.c
and you'll get all matches for 'somestring' in any C source files.
Once done grepping, just do a fg (foreground) command and boom - back to VIM.
vimgrep will work, but if the source happens to be in Git then you can use tpope's excellent https://github.com/tpope/vim-fugitive plugin, which exposes :Ggrep which hangs off git grep for more flexibility.
If it's specific entities you're looking for (functions, variables, etc) the integration with ctags is probably of interest to you as well.
Sounds like you might like to have a look at vim tag search functionality combined with ctags. ctags is an utility that can be used to generate an index (a tags file) of various language objects for source code (full project tree, not just a directory). In vim a tag is sort of identifier that can be jumped to or searched for.
See vim documentation:
:help tagsrch
Or:
http://vimdoc.sourceforge.net/htmldoc/tagsrch.html#ctags
When using Vim, and given a directory filled with code (e.g. ~/trunk/) with a number of sub-directories, is there a way to grep/search for instances of text/regexes across the entire source code?
At the moment I use:
:lcd ~/trunk
:grep "pattern" *.py */*.py */*/*.py */*/*/*.py
(Obviously I'm limiting this to Python files, which is a different issue, but it's noteworthy that ideally I'd like to limit the searches to files with specific extensions.)
:vimgrep "pattern" ~/trunk/**/*.py
:copen 20
If you have quite a big project I'd recommend to you to use cscope and vim plugins. Here is one designed to handle big projects: SourceCodeObedience
There is a brief description of how to grep text using SourceCodeObedience.
I use grep directly for that.
grep -IirlZ "pattern" .|xargs -0 vim
-I: ignore binary
-i: ignore case
-r: recursive
-l: print file name only
-Z: print \0 after each file name
You may want to check out :vimgrep and :grep in the vim documentation. :vimgrep uses vim's own pattern searching functionality and reads all files into vim buffers. :grep by default links to an external grep utlity on your system, but you can change the behavior by setting the grepprg option. Here's a link to the online docs:
http://vimdoc.sourceforge.net/htmldoc/quickfix.html#grep
There's more info on that and also some other options in the tip on "Find in files within Vim" at the Vim Tips Wiki:
http://vim.wikia.com/wiki/Find_in_files_within_Vim
Set grepprg to Ack, then use :grep to search with Ack.
Or use ctags.
Ag is good option to search the pattern recursively.
https://github.com/rking/ag.vim
You could use :vimgrep i.e.:
:vimgrep /pattern/ **/*.py
Check this Vim Tip:
Find in files within Vim
Also give a look to grep.vim, it's a plugin that integrates the grep, fgrep, egrep, and agrep tools with Vim and allows you to search for a pattern in one or more files and jump
to them...
You can generate a source code index using ctags and then VIM can use this to navigate seamlessly through your code base. It's source code aware in that you can jump directly to method declarations etc.
You need to regenerate the ctags files every so often, but you can do that as part of your make process. It should be pretty fast unless your code base is sizeable.
Nobody's mentioned it, but I use tend nowadays to use ripgrep