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 


!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?


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"


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' %`
  • 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)? – Léo Léopold Hertz 준영 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' % – Léo Léopold Hertz 준영 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. – Léo Léopold Hertz 준영 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 %


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: Does the percent sign % after the "sed" means: apply the sed command to all lines of the file? – Léo Léopold Hertz 준영 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 <==
source b
source c

==> b <==

==> c <==

$ grep file a $(grep source a | awk '{print $2}')
  • If I understand correctly, you mean the following: !grep source * (grep source * | awk '{ print }') . I run it unsuccessfully. – Léo Léopold Hertz 준영 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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