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.
-c\b
see How do I perform a exact match in man page?