Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
714 views
in Technique[技术] by (71.8m points)

search - Exact string match in vim? (Like 'regex-off' mode in less.)

In vim, I often want to search on a string with finicky characters which need escaping. Is there a way I can turn off the meaning of all special characters, kind of like regex-off mode in less, or fgrep?

I am dealing with particularly hairy strings; here's an example:

((N/N)/(N/N))/N

Not having to escape any characters to do a search in vim would be a major timesaver.

V in Vim helps with some metacharacters, but critically not / or .


Thanks all! In the end, I added this to my .vimrc:
command! -nargs=1 S let @/ = escape('<args>', '')
nmap <Leader>S :execute(":S " . input('Regex-off: /'))<CR>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Depending on the exact string you're searching, the V prefix will probably do the trick.
See :help V:

after:    v       m       M       V         matches ~
                'magic' 'nomagic'    
          $        $        $        $         matches end-of-line
          .        .        .       .         matches any character
          *        *        *       *         any number of the previous atom
          ()       ()     ()     ()       grouping into an atom
          |        |       |       |         separating alternatives
          a       a       a       a         alphabetic character
          \       \       \       \         literal backslash
          .       .       .        .          literal dot
          {       {        {        {          literal '{'
          a        a        a        a          literal 'a'

So if I have a string hello.*$world, I can use the command /V.*$ to find just .*$ -- the only part of the string that should need escaping is another backslash, but you can still do grouping, etc., by escaping the special symbol.


Another command you can use to "avoid" forward slashes is:
:g #V((N/N)/(N/N))/N#   

The :g command is a global search, noting that:

:[range]g[lobal]/{pattern}/[cmd]
                        Execute the Ex command [cmd] (default ":p") on the
                        lines within [range] where {pattern} matches.

Instead of the '/' which surrounds the {pattern}, you can use any other
single byte character, but not an alphanumeric character, '', '"' or '|'.
This is useful if you want to include a '/' in the search pattern or
replacement string.

So where I used a #, you could use a ?, @, or whatever other character meeting the above condition. The catch with that :g command is that it expects a command at the end, so if you do not have a trailing space after the final character, it won't perform the search as you would expect. And again, even though you're using V, you'll still have to escape backslashes.

If that still doesn't cut it for you, this Nabble post has a suggestion that takes a literal string with embedded backslashes and other special Vim characters, and claims to search for it without a problem; but it requires creating a Vim function, which may or may not be okay in your environment.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...