Suppose we have a file with the contents:
123 a shouldmatch 456 b shouldmatch 111 c notmatch
And we like to match
123 a shouldmatch 456 b shouldmatch
with regex
.*shouldmatch
If you have only one match per line, you can use readfile() and then loop through the lines and check each line with matchstr() . [1]
function! Test001() let file = readfile(expand("%:p")) " read current file for line in file let match = matchstr(line, '.*shouldmatch') " regex match if(!empty(match)) echo match " your command with match endif endfor endfunction
You can put this function in your ~/.vimrc and call it with call Test001() .
[1] http://vimdoc.sourceforge.net/htmldoc/eval.html#matchstr%28%29
source share