I have encountered some situations where I need to check if a value matches a regex in a list of regexes. For instance, say I have a list of regexes which match filetypes:

['gundo', 'nerdtree', 'git*']

I need to find out if the filetype of the current file matches any of these regexes. The way I would do it currently would look like this:

fun! IsIgnored()
    for ignored_ft in ignored_filetypes
        if &filetype =~ ignored_ft
            return 1
    return 0

However, it would be nice if there was a shorter way of doing this. Does vim have a function to do this, or do I just have to stick with the for loop?

  • 2
    There's a flaw in what you're trying to do. You're using globs, but treating them as regular expressions. As a regular expression, git* matches gi, git, gitt, etc. This is easily fixed by using git.* instead. Be careful not to use glob syntax, as is common when looking at file names and file types.
    – tommcdo
    Apr 27, 2015 at 12:36

2 Answers 2


I did define lh#list#match_re() (based on a loop for the purpose of finding which regex from a list is matched by a text).

However, in you case, why don't you just test &ft =~ join(ignored_filetypes, '\|') ?

  • Would this be faster than @Carpetsmoker's answer? Apr 26, 2015 at 23:52
  • @EvergreenTree Probably, yes. But I suspect the difference will be minimal (probably unmeasurable) in most circumstances. Go for the solution you find the most readable. Apr 27, 2015 at 0:00
  • I've no idea. carpetsmoker's trick with index+map would probably be faster than the loop (that I use on my library). However, the join() trick has good chances to be the faster one. Moreover, you can precompute the regex if the list of filetypes doesn't change. Apr 27, 2015 at 0:17

This should do the same:

fun! IsIgnored()
    return index(map(['gundo', 'nerdtree', 'git*'], '&filetype =~ v:val'), 1) > -1

We first map the expression &filetype =~ v:val to every entry in our array, this will return either [0, 0, 0] if there are no matches, or [0, 1, 0] if (for example) nerdtree matches.
We then use index() to check if the array that map() returns has an entry with a value of 1 (index() returns -1 on no matches).

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.