Vim: Match character with a word on the left? - vim

I want to match the following ... in Vim:
Um...yeah.
No...
I know in most programming languages you can do this:
/\b\.\.\./g
How about Vim? How can I match the ... in this situation?

You can try the following one:
/\v\w\zs[.]{3}
It matches three dots that follow any word character, you can use some kind of quantifiers after the \w to match a minimum number of letters, but I hope you get the idea.

Related

Searching for an exact match with a singular digit

I'm trying to search for only a singular digit in vim by itself. For example, if there are two sets of digits 1 and 123 and I want to search for 1, I would only want the singular 1 digit to be found.
I have tried using regular expressions like \<1> and \%(a)#
You almost had the right solution. You want:
\<1\>
This is because each angled bracket needs to be escaped. Alternatively, you could use:
\v<1>
The \v flag tells vim to treat more characters as special without needing to be escaped (for example, (){}+<> all become special rather than literal text. Read :h /\v for more on this.
A great reference for learning regex in vim is vimregex.com. The \<\> characters are explained in 4.1 "Anchors".
If you want to match text like 1.23 this is possible too. Two different approaches:
Modify the iskeyword option so that it includes .. This will also affect how w moves
Use \v<1(\d|.)#!, which basically means "a 1 at the beginning of a word, that isn't followed by some other digit or a period."

How to write zero-width assertions in vim?

Here is the raw material I’m working with:
line1=a1 abc
line2=abc
line3=aba
line4=cbc
i want to match lines which do not contain character string of "abc" ,the result is :
line3=aba
line4=cbc
how can i get it in vim? maybe the expression is something such as (?!abc) in perl ,i am not sure how to write the regular expression in vim.
To match lines not ending with abc you could write the expression in two ways. My preferred is With very magic
/\v.*(abc)#!/
And with no very magic:
/.*\(abc\)\#!/
I recommend you to take some time to read:
:help magic
From Power Of G:
Delete all lines that do not match a pattern.
:g!/<pattern>/d
Of course, you can replace the d at the end to do something other than deleting the line...
It seems you're familiar with Perl regular expressions. You will probably be interested in :help perl-patterns where you can Vim equivalents for common Perl regex patterns. There, you can see that for a zero-width negative look-ahead, you want \#!.
For other zero-width patterns, including some not listed at :help perl-patterns, see :help /\#= and following. Also useful are \zs and \ze which can avoid some more complex zero-width matches in many cases.

How to exclude capitalized words from spell checking in Vim?

There are too many acronyms and proper nouns to add to the dictionary. I would like any words that contains a capital letter to be excluded from spell checking. Words are delimited by either a whilespace or special characters (i.e., non-alphabetic characters). Is this possible?
The first part of the answer fails when the lowercase and special characters surround the capitalized word:
,jQuery,
, iPad,
/demoMRdogood/
[CSS](css)
`appendTo()`,
The current answer gives false positives (excludes from the spellcheck) when the lowercase words are delimited by a special character. Here are the examples:
(async)
leetcode, eulerproject,
The bounty is for the person who fixes this problem.
You can try this command:
:syn match myExCapitalWords +\<[A-Z]\w*\>+ contains=#NoSpell
The above command instructs Vim to handle every pattern described by \<[A-Z]\w*\> as part of the #NoSpell cluster. Items of the #NoSpell cluster aren’t spell checked.
If you further want to exclude all words from spell checking that contain at least one non-alphabetic character you can invoke the following command:
:syn match myExNonWords +\<\p*[^A-Za-z \t]\p*\>+ contains=#NoSpell
Type :h spell-syntax for more information.
Here is the solution that worked for me. This passes the cases I mentioned in the question:
syn match myExCapitalWords +\<\w*[A-Z]\K*\>+ contains=#NoSpell
Here is an alternative solution that uses \S instead of \K. The alternative solution excludes characters that are in the parenthesis and are preceded by a capitalized letter. Since it is more lenient, it works better for URLs:
syn match myExCapitalWords +\<\w*[A-Z]\S*\>+ contains=#NoSpell
Exclude "'s" from the spellcheck
s after an apostrophe is considered a misspelled letter regardless of the solution above. a quick solution is to add s to your dictionary or add a case for that:
syn match myExCapitalWords +\<\w*[A-Z]\K*\>\|'s+ contains=#NoSpell
This was not part the question, but this is a common case for spell checking process so I mentioned it here.

What vim pattern matches a number which ends with dot?

In PDP11/40 assembling language a number ends with dot is interpreted as a decimal number.
I use the following pattern but fail to match that notation, for example, 8.:
syn match asmpdp11DecNumber /\<[0-9]\+\.\>/
When I replace \. with D the pattern can match 8D without any problem. Could anyone tell me what is wrong with my "end-with-dot" pattern? Thanks.
Your regular expression syntax is fine (well, you can use \d instead of [0-9]), but your 'iskeyword' value does not include the period ., so you cannot match the end-of-word (\>) after it.
It looks like you're writing a syntax for a custom filetype. One option is to
:setlocal filetype+=.
in a corresponding ~/.vim/ftplugin/asmpdp11.vim filetype plugin. Do this when the period character is considered a keyword character in your syntax.
Otherwise, drop the \> to make the regular expression match. If you want to ensure that there's no non-whitespace character after the period, you can assert that condition after the match, e.g. like this:
:syn match asmpdp11DecNumber /\<\d\+\.\S\#!/
Note that a word is defined by vim as:
A word consists of a sequence of letters, digits and underscores, or a
sequence of other non-blank characters, separated with white space
(spaces, tabs, ). This can be changed with the 'iskeyword'
option. An empty line is also considered to be a word.
so your pattern works fine if whitespace follows the number. You may want to skip the \>.
I think the problem is your end-of-word boundary marker. Try this:
syn match asmpdp11DecNumber /\<[0-9]\+\./
Note that I have removed the \> end-of-word boundary. I'm not sure what that was in there for, but it appears to work if you remove it. A . is not considered part of a word, which is why your version fails.

searching whole word in Vim (dash character)

I know for searching a whole word I should use /\<mypattern\>. But this is not true for dash (+U002d) character and /\<-\> always fails. I also try /\<\%d45\> and it fails too. anyone know the reason?
Edit2: As #bobbogo mentioned dash is not in 'iskeyword' so I add :set isk+=- and /\<-\> works!
Edit1: I think in Vim /\<word\> only is valid for alphanumeric characters and we shouldn't use it for punctuation characters (see Edit2). I should change my question and ask how we can search punctuation character as a whole world for example I want my search found the question mark in "a ? b" and patterns like "??" and "abc?" shouldn't be valid.
\< matches the zero-width boundary between a non-word character and a word character. What is a word character? It's specified by the isk option (:help isk).
Since - is not in your isk option, then - can never start a word, thus \<- will never match.
I don't know what you want, but /\>-\< will match the dash in hello-word.
Could always search for the regex \byourwordhere\b
As OP said. In order to include dash - into search just execute:
:set isk+=-
Thats all.
Example: When you press * over letter c of color-primary it will search for entire variable name not just for color.

Resources