Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 3 down vote accepted
+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' %`
share|improve this answer
    
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)? –  Masi Jun 29 '09 at 16:15
    
Yes, that's what it means. The '-n' tells sed only to output what's explicitly stated by the p command. –  chmeee Jun 29 '09 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' % –  Masi Jun 29 '09 at 16:20
    
I updated my answer to look for source lines as well –  chmeee Jun 29 '09 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. –  Masi Jul 1 '09 at 18:39

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 %

share|improve this answer

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.

share|improve this answer
    
@Evgeny: Does the percent sign % after the "sed" means: apply the sed command to all lines of the file? –  Masi Jul 1 '09 at 18:48
    
No, the % gets substituded with the file name vim is editing. –  Evgeny Apr 6 '11 at 13:00

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
share|improve this answer
    
If I understand correctly, you mean the following: !grep source * (grep source * | awk '{ print }') . I run it unsuccessfully. –  Masi Jun 3 '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 3 '09 at 17:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.