Learn Essential Vim Skills
with Drew Neil, author of Practical Vim
vimgrep
category applies to 4 screencasts and 2 articles:
The :Subvert command lets us create a particular style of regular expressions with ease. It’s great for matching irregular singular and plural words in plain English and also for variable names that come in snake_case and MixedCase forms.
This is part one of a three-part series on Tim Pope’s abolish plugin.
Watch screencastThe * command searches for the word under the cursor. That makes sense in Normal mode, but from Visual mode it would be more useful if the star command searched for the current selection, rather than the current word. We can add this feature to Vim using the visual star search plugin.
Vim doesn’t have a built-in command for project-wide find and replace operations, but we can perform this task by combining primitive Ex commands such as :substitute, :argdo, and :vimgrep. We’ll look at two possible strategies: first using the arglist, then the quickfix list.
The vimgrep command uses Vim’s native regular expressions to search the contents of multiple files. There are several ways that we can specify the list of files to look inside, including * and ** wildcards. It would be handy if we could instruct vimgrep to look inside all of the files in the current project, excluding those listed in the .gitignore file. That’s where the git ls-files command comes in.
I love the way that ack let’s me specify the files to search inside. For starters, ack does the right thing by ignoring the contents of VCS directories, backup files, core dumps etc., which gives a good signal to noise ratio. On top of that, ack provides a convenient syntax for specifying filetypes to include or exclude from the set (see ack --help-types). I can target ruby files only with the --ruby option, or everything but ruby files with --noruby.
vimgrep is Vim’s built-in command for searching across multiple files. It’s not so fast as external tools like ack and git-grep, but it has its uses. vimgrep uses Vim’s built-in regex engine, so you can reuse the patterns that work with Vim’s standard search command.