Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

when using vim, man, or less, I want to do some exact match in regrex

for example, when using man, I want to check the argument '-c'

if I use

  /'-c'

the matching could be -cim -covert......blabla

but I only want to match '-c'

how to do the exact matching? thanks!

share|improve this question

2 Answers 2

A shorter vim expression to ensure no characters precede or follow

/\(^\|\s\)\@<=-c\>

The man/less equivalent:

/(^|\s)-c\b

Additional explanation (Vim):

Probably the most useful part of the Vim regular expression is -c\> which simply says "Look for '-c', but only if no letters come right after". Most of the time, you can probably get away with just searching with /-c\>, but for the sake of completeness, I included an expression to check what comes before the '-c'. That expression is \(^\|\s\)\@<=, which looks complicated just because Vim's regular expression syntax is a bit more verbose (in that it requires you to escape grouping parentheses and the 'or' pipe). The expression \(^\|\s\) means "The beginning of a line or a whitespace character". When you put \@<= after it, it means "Don't really match that, just make sure it comes before the next part of the regular expression" (which is only really useful for search-and-replace operations). Practically speaking, you likely don't need the \@<= part. You could shorten it further by omitting that and adding \v, which tells Vim you won't be escaping fancy syntax. That would look like /\v(^|\s)-c>.

Explanation of the man/less equivalent:

Much simpler. For the regular expression syntax used by less (which is the default man pager), \b is the same as Vim's \>, and you don't need to escape the parentheses or pipe character. It just looks for the string '-c' which occurs at the beginning of the line or immediately after a whitespace character and makes sure no other letters come after.

share|improve this answer

In vim, use

/\(^\|\s\)-c\($\|\s\)

In less:

/(^|\s)-c($|\s\)

You could use isk and word boundaries in vim, but this would also match other things that might be options; it's safer to explicitly look for blanks.

share|improve this answer
    
I tried in the man page, it doesnt work –  user138126 Mar 6 '13 at 16:36
    
@user138126 See my update, should better fit your use case. –  Chris Down Mar 6 '13 at 16:47
    
wow, it is so complex, can you explain it a bit or are there any good related articles on how these scripts can represent '-c' –  user138126 Mar 6 '13 at 16:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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