Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a file includes some content

Tags:

vim

Thanks to a previous answer, I can test if a file exists using

if !empty(glob("filename"))
  ...
endif

I now want to check if the file filename contains text text.

If possible, I want to do this using native vimscript only, not calling system's grep etc commands.

like image 329
Dogbert Avatar asked Aug 06 '12 05:08

Dogbert


2 Answers

if filereadable("filename") && match(readfile("filename"),"text")
   ...
endif

See :help function-list for a high-level overview of vimscript functions.

like image 96
Conner Avatar answered Nov 19 '22 20:11

Conner


I use:

if match(readfile(expand("%:p")),"pattern")!=-1
   # content
endif

to check if a file contains a pattern in vimscript

like image 3
edi9999 Avatar answered Nov 19 '22 20:11

edi9999