1

I have files which I source in my files. I want to search them, when I am at a file which have these sources.

I run unsuccessfully

!bufdo grep source 

and

!bufdo grep source %

Example of .zshrc which sourced PATHs I want to grep

# ~/bin/shells/Zsh/describtion
# ~/bin/shells/Zsh/bugs
# ./bin/shells/zsh/alphaOptions

. /Users/masi/bin/shells/.personal/ssh
. /Users/masi/bin/shells/.personal/shellVariables
. /Users/masi/bin/shells/.personal/alias

. /Users/masi/bin/shells/externalPrograms

. /Users/masi/bin/shells/codingSettings

# . /Users/masi/bin/shells/OS/ubuntu/alias

# ~/bin/shells/Zsh/sources

Let's assume you are at my .zshrc. You want to search the word "manual" in all sourced files in Vim.

How would you search the word manual in only the sourced files?

4 Answers4

3

This worked for me:

:! grep manual `sed -n 's/^\. \(.*\)/\1/p' %`

The sed part looks for source files and gives them as arguments to grep.

To avoid having to type this everytime you can create a command like this:

:command! -nargs=1 SourceGrep :! grep <args> `sed -n 's/^\. \(.*\)/\1/p' %`

Be careful when searching for more than one word, you'll have to use quotes. For example:

:SourceGrep "sudo ssh"

EDIT:

To search for lines with 'source' as well as '.':

:! grep manual `sed -rn 's/^(\.|source) (.*)/\2/p' %`

The command will look like this:

:command! -nargs=1 SourceGrep :! grep <args> `sed -rn 's/^(source|\.) (.*)/\2/p' %`
chmeee
  • 7,370
  • 3
  • 30
  • 43
1

I'm not sure if I understood your question correctly, but if you want to run external command from vim and pass it the content of the file you are currently editing, you can use something like this:

:!grep sometext %

ipozgaj
  • 1,081
  • 10
  • 10
1

Assuming you have your .zshrc file open, and you want to grep something in all of the files source in it. You can do this from vim like this:

!sed -ne 's/^\. \(.*\)/\1/gp' % | xargs grep foobar

Replace "foobar" with whatever it is you want to grep.

Evgeny
  • 599
  • 5
  • 10
0

Assuming your main file is called 'mainfile', you can run something like:

$ grep searchstring mainfile $(grep source mainfile | awk '{print $2}')

(I assumed you use source <file> inside your mainfile for sourcing)

Update: See an example:

$ head a b c
==> a <==
afile
source b
source c

==> b <==
bfile

==> c <==
cfile

$ grep file a $(grep source a | awk '{print $2}')
a:afile
b:bfile
c:cfile
yhager
  • 133
  • 5
  • If I understand correctly, you mean the following: !grep source * `(grep source * | awk '{ print }')` . I run it unsuccessfully. – Léo Léopold Hertz 준영 Jun 03 '09 at 15:23
  • I am sorry, my response was for shell commands. I assume you can use that as a base for something to run from vi though. I added an example to my response. – yhager Jun 03 '09 at 17:31