I have a tags plugin that I'm adding features to. In order to test that it is working correctly, I need to examine the tags inside a tags file. I am trying to make sure the correct tags appear in the file and they are not there too many times. My test code is in VimL/Vimscript, so this is a VimL question.

One way to do this is to have the tags file open in a Vim buffer, and then use the search() function to search for that text. Then I just check to see if it appears at the beginning of the line. The problem is search() accepts a regular expression, which is not what I am providing it.

I could bring in each line as a string and examine them one at a time, but that sounds slow and like more work compared to getting search() to work for me.

How can I get search() to work without regular expressions, or in some other way find the tags matching a non-regular expression search in a tags file?

  • What do you call a "non-regular expression"?
    – romainl
    Jul 15, 2016 at 21:19
  • @romainl a string? Jul 15, 2016 at 21:32
  • 1
    :h \V "very nomagic"? Jul 16, 2016 at 4:11
  • Both foo and [bfx]o\{2} are regular expressions. You are creating issues where there are none.
    – romainl
    Jul 16, 2016 at 7:39
  • @SatoKatsura That very no magic thing will be super helpful both for this and to fix bugs in my search plugin! Jul 16, 2016 at 19:55

1 Answer 1


You can use a function, that would search for the string literally. Something like this should work:

func! SearchLiterally(pat, flags)
    return search('\V'.escape(a:pat, '\\'), a:flags)

You can even do that:

func! ReplaceSearch()
    let cmd=getcmdtype()
    if cmd =~# '[?/]'
        return SearchLiterally(getcmdline(), cmd)
        return getcmdline()

func! SearchLiterally(pat, search)
    return '\V'.escape(a:pat, '\\'. a:search)

cmap <F7> <C-\>eReplaceSearch()<CR>
  • That SearchLiteral() function is totes digi!. It helps to illustrate how to apply the very nomagic trick referred to by @Sato Katsura. I will be able to use this to address the issue in the tags plugin and to fix bugs in my search plugin! I might mark it as the answer once I get a chance to test it and look into more alternatives. Jul 16, 2016 at 19:58
  • After examining the help for very no magic :h \V, I am not entirely sure this SearchLiteral function will always work. The help talks about backslash being an exception, which is accounted for here, as well as the terminating character (/ or ?). I don't understand what a terminating character is, but could that mess up this function? Jul 16, 2016 at 23:10
  • with the termination character / or ? is meant, which is the character that starts and ends the search. However, this does not matter for the search() function and in the second example it is escaped as well. Jul 17, 2016 at 4:51
  • Even if that is true, I want to understand it. In what context would a character end a search? Normally doesn't a user press enter to end a search? Jul 17, 2016 at 12:17
  • @still_dreaming_1 What do you mean with "even if that is true"? It's all there in the help. Jul 17, 2016 at 12:32

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.