6

One features I like with Visual Studio is the ability to search in open files only. For example, if I recently did changes to some files and I would like to trace those changes, I might search for a certain word, but only in those files to avoid getting a large list of necessary matches.

Is this possible with Vim?! What I am interested in is being able to open the files I have changed so for using:

gvim `git diff --name-only`

then search those files for what I want.

Rafid
  • 16,077
  • 21
  • 63
  • 98
  • There are many ways to do that. Can you be a little more specific to the kind of output/result you are expecting? – Eelvex Feb 27 '11 at 08:25
  • I thought the message is clear. Say I have 5 open files. If I want to use vimgrep/grep to search those 5 files, I have to list them by name, which is quite long task. I want vimgrep/grep to search in opened files (i.e. buffers) only. – Rafid Feb 27 '11 at 08:35
  • you can `:vim /pattern/ \`git diff --name-only\`` or `:bufdo :%s/ ...` or ... – Eelvex Feb 27 '11 at 08:38

2 Answers2

2

A nice way to do that is to use vim's internal grep command (:vim):

:vim /pattern/ `git diff --name-only`
:copen

This will open a small window (called quickfix) with the search results and links to open the corresponding files (they don't have to be open).

Eelvex
  • 8,397
  • 22
  • 40
1

If you want vim to open up all the files in their own buffers for files that match your diff, you could try this:

gvim $(grep -l pattern $(git diff --relative --name-only))

git diff --relative --name-only shows the changed files in the index but with filenames relative to the current working directory.

grep -l pattern <list of files> will report the filenames that contain a match on pattern. (Note that the pattern just has to exist in the files, not in the git diff output.)

POSIX $() instead of backticks makes using nested commands possible.

sarnold
  • 96,852
  • 21
  • 162
  • 219
  • nested commands are possible with backticks also: `echo echo \echo abc\` (replace with actual backticks). – ZyX Feb 27 '11 at 15:40
  • @ZyX, ha, never thought to escape backticks to nest them before. :) Every day something new. – sarnold Feb 28 '11 at 01:01