1

I'm trying to make a vimscript that will read the .gitignore file, return it's values as a List and check for paths that has wildcards eg: /vim/snippets/*, vim/**/*.

let g:ignored = readfile("/home/user/Projects/dotfiles/.gitignore")

let c=0
while c < len(g:ignored)

  let g:ignoredPath = ignored[c]
  let c += 1

  echo g:ignoredPath

endwhile

I want to check g:ignored variable for possible values with wildcards but how can I do that? I'm guessing I have to use regex in an execute or something. But I don't know the right syntax at all.

4
  • after the readfile you already have a list of all entries. You can use the match() function to check which item matches a certain regex Commented Nov 1, 2018 at 14:36
  • @ChristianBrabandt sorry I didn't clarrified my intention, but I want to save the matching value in a variable.. and it seems impossible using match() (correct me if i'm wrong) as it only returns integer. Commented Nov 1, 2018 at 15:12
  • 2
    have a look at :h matchstr then.
    – B.G.
    Commented Nov 1, 2018 at 15:17
  • @DoktorOSwaldo thank you so much that did the trick! could you make this an answer so I can accept it? Commented Nov 1, 2018 at 15:29

1 Answer 1

2

The Function you are searching is matchstr it returns the matched string or if the pattern is not found, an empty string.

For your example you should note: if the expression given to matchstr is a List the matching item is returned and the type isn't changed. So it is possible that the returned item isn't a string if your list consist of other datatypes.

but as always you can read all that in :h matchstr.

Note: There is also :h matchlist which can work with capturing groups (in vim called submatches) and :h matchstrpost which also returns the start and end position of the match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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