tags:

views:

336

answers:

2

I would like to get an AND -operator to the following Vim command

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

such that I can use the command to find sources at

# source <path>
# . <path> 
source <path>
. <path>

I am not sure whether it is possible or not to get AND -operator to SED or Vim.

How can you add an AND -operator to the SED command?

+1  A: 

You can just use Vim regular expressions to find the <path> name, instead of using sed:

:command! -nargs=1 GrepSource :exe printf("!grep '<args>' '%s'", matchstr(getline('.'), '^#\=\s*\%(source\|\.\)\s*\zs\f\+'))

I think that command should do what you want. You can use the next command to test what is happening ('exe' is changed to 'echo'):

:command! -nargs=1 GrepSourceTest :echo printf("!grep '<args>' '%s'", matchstr(getline('.'), '^#\=\s*\%(source\|\.\)\s*\zs\f\+'))

The command must be used on a line in one of the following forms:

# source <somefile>
# . <somefile>
source <somefile>
. <somefile>
too much php
I could not get your commands to work. Please, say if your commands work in your machine. --- I use Vim 7.2, OS X Leopard.
Masi
I also use 7.2 on OS X 10.5. What output did you get from using the second command (GrepSourceTest)? Make sure your cursor is on a line something like 'source <somefile>' or '. <somefile>' or '# . <somefile>'
too much php
Your command is excellent! --- I did not have in mind such a command. --- I was looking for a command which greps all sourced PATHs. To get that we need to filter the wanted lines and give them to Grep. --- I am not sure how we can filter/search such lines in an one-liner.
Masi
Your command is better than I was aiming at. --- I can grep all files in current directory by `:!grep -r ~/bin/wanted-dir`. The weakness of my command is that it is impractical in analysing projects which have long files with many sources of files at utterly different locations.
Masi
+2  A: 

I'm not clear what you are trying to achieve/searching for. Maybe if you elaborate what exactly are you trying to do and what the AND is supposed to do we could help more.

This solution executes from vim command:

grep PATTERN /some/path/*

If you are looking for files in a directory you would use:

ls /some/path | grep PATTERN

You can update relevant pieces of the command.

Anyway...

From your script I assume you have a line specifying path to a directory which you want to grep for something, e.g. you want to search path for PATTERN, you position cursor on the line with path, then you want to extract the path and grep it for the pattern with your command, example line in file:

source /some/path

This command should do what you expect:

:command! -nargs=1 GrepSource :exe printf("!grep '<args>' '%s'/*", substitute(
getline('.'), "\\%(source\\s*\\)\\(.*\\)$", "\\1",""))

With cursor on the line with path starting with word source you can execute the above command:

:GrepSource PATTERN

This will search files in '/some/path/' for word PATTERN.

I assume paths in your file don't end with slash '/' and I assume you want to search in files so I append '/*' at the end of path to search, ending with command:

grep PATTER /some/path/*

I have used substitute command that replaces

source /some/path

with

/some/path

Which is then used in printf to create the actual command.

stefanB
@stefanB: I run unsuccessfully your command. --- Its purpose seems to same as `too much php`'s. The difference is the use of `substitute` and regexes.
Masi