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 Answers 4

3
+100

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' %` 
7
  • Does the code "'s/^\. (.*)/\1/p" mean #1. find the start of a line which has a dot and a [space]+word after that #2. replace the match with the the reference to the first parameter (\1)? Commented Jun 29, 2009 at 16:15
  • Yes, that's what it means. The '-n' tells sed only to output what's explicitly stated by the p command. Commented Jun 29, 2009 at 16:20
  • I use also the word "source". I need OR -operator for SED. I did not find one for SED. I run too unsuccessfully: % sed 's/sa\|he/x/g' % Commented Jun 29, 2009 at 16:20
  • I updated my answer to look for source lines as well Commented Jun 29, 2009 at 16:44
  • @chmeee: Thank you for your answers! --- I noticed that Vim's ex -mode has a different syntax than Sed: you need to escape the brackets in Vim, while in SED, you do not need to. Commented Jul 1, 2009 at 18:39
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 %

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.

2
  • @Evgeny: Does the percent sign % after the "sed" means: apply the sed command to all lines of the file? Commented Jul 1, 2009 at 18:48
  • No, the % gets substituded with the file name vim is editing. Commented Apr 6, 2011 at 13:00
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 
2
  • If I understand correctly, you mean the following: !grep source * (grep source * | awk '{ print }') . I run it unsuccessfully. Commented Jun 3, 2009 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. Commented Jun 3, 2009 at 17:31

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.