Temporarily declare a word as wrong in vim spellchecker - vim

I'm writing a document with vim and latex. I recently noticed a word that I use ("tree"), but which is incorrect in the context of my document. I corrected it on the whole document. However, I have the habit to use this word instead of the correct one, when writing.
Is it possible to declare this word as a temporary wrong word (until the buffer is closed, this is enough), to make the spell-checker highlight it?
I have tried to mark it as definitive wrong word. This is not a solution as "tree" is in fact a correct word and should not be highlight in other documents. I also tried to search it: my vim is configured to automatically highlight it when I write it. This is a half-solution, as it needs that my last search be "tree", and handles difficultly many words if I have really wrong habits.

Vim spell cheat sheet:
]s / [s - next / previous misspelled word
]S / [S - next / previous bad word (i.e. as above, but ignore rare words and the like)
zg - add good word to the first dictionary in spellfile; remove it with zug
zG - add good word to the internal wordlist; remove it with zuG
zw - add bad word to the first dictionary; remove it with zuw
zW - add bad word to the internal wordlist; remove it with zuW
z= - suggest correctly spelled word.
According to the documentation,
The internal word list is used for all buffers where 'spell' is set. It is
not stored, it is lost when you exit Vim. It is also cleared when 'encoding'
is set.
So this is what you are looking for.
Please see :help spell-quickstart for more details.

You can set a match in your buffer, which will vanish when you close it.
:match Error /tree/
To only match the whole word "tree", use \< to match the beginning of the word and \> to match the end.
:match Error /\<tree\>/
:match documentation: http://vimdoc.sourceforge.net/htmldoc/pattern.html#:match

Related

Select first char up to first non camelCase or non upper case char or up to first snake case _ in vim

I used this map:
map ,w v/\([^ a-z0-9]\|[^ A-Z0-9]\)*<cr>h
the idea is to select
in the words
mysuperTest
MYSUPER_TEST
mysuper_test
to always select the part that says mysuper
but it doesnt work, not sure why
I would use something like the below:
nnoremap ,w v/\C\%#.\([a-z]\+\<bar>[A-Z]\+\)\zs<cr>h
One point to notice is that in a mapping you need to use <bar> (or escape | with an extra backslash) since otherwise | is recognized as a command separator (see :help map-bar.)
Another one to notice is that you want the match to start at the first character outside the word (so you'll land at the end of the word with the h). The visual selection will expand to the start of the match in a search. I suggest using \zs to set the start of the match explicitly (see :help /\zs.)
Finally, beware of 'ignorecase' or 'smartcase' settings. Use \C to explicitly request a case-sensitive match (see :help /\C.)
I also like the idea of using a stronger anchor for the start of the match, so I'm using \%# to match the current cursor position (see :help /\%#), so you're always sure to match the current word only and not end up wandering through the buffer.
Putting it all together:
\C Case-sensitive search
\%# From cursor position
. Skip first character
\( Either one of:
[a-z]\+ One or more lowercase letters
\| (\<bar>) Or:
[A-Z]\+ One or more uppercase letters
\) End group
\zs Set match position here
I'm skipping the first character under the cursor, since in a CamelCase word, the first character won't match capitalization of the remainder of the word.
I kept your original idea of finding the first character after the word then using h to go back one to the left. But that might be a problem if, for example, the word is at the end of the line.
You can actually match the last character of the word instead with something like [a-z]\+\zs[a-z], which will set the start of the match on the last lowercase character. You can do this for both sides of the group (you can have more than one \zs in your pattern, last wins.) If you structure your match that way, you won't need the final h to go back.
I didn't handle numbers, I'll leave those as an exercise to the reader.
Finally, consider there are quite a few corner cases that can make such a mapping quite tricky to get right. Rather than coming up with your own, why not look at plug-ins which add support for handling CamelCase words that have been battle-tested and will cover use cases a lot more advanced than the simple expression you're using here?
There's the excellent vim-scripts/camelcasemotion by Ingo Karkat which sets up a ,w mapping to move to the start of the next CamelCase word, but also i,w to select the current one. You can use powerful combinations such as v3i,w to visually select the current and next two CamelCase words.
You might also check Tim Pope's tpope/vim-abolish which, among other features, defines a set of cr mappings to do coercion from camelCase to MixedCase, snake_case, UPPER_CASE, etc. (Not directly about selecting them, but still related and you might find it useful.)

Vim: recognize character sequences as part of "whole word"

This has been bugging me for a while: I'm writing code that uses verilog-auto which means I'm editing in a verilog file with perl snippets injected into comment sections. One very useful thing that I like to do in Vim is to search for the whole word under the cursor with * and #. However, with perl syntax that contains variable names such as ${w} and $w, these shortcuts don't work.
I don't want to add $, { and } to my "keywords" list as there are many instances where I don't want these to count as part of a whole word. For instance, in verilog concatenation: {sig1,sig2[1:0]}, I wouldn't want {sig1 to be searched for as a whole word.
Is there a way to get "whole word" to recognize sequences via a regex or something? So only ${[a-z]+} or $[a-z]+ gets recognized as "keywords".
Either that or a separate keyboard shortcut that can let me search for the pattern under the cursor.
Here's a really ugly hack, but it works:
nnoremap * viW:s/\%V\$*{*\a*}*/\=setreg('a', submatch(0))/n<cr>/<C-r>a<cr>n
nnoremap # viW:s/\%V\$*{*\a*}*/\=setreg('a', submatch(0))/n<cr>/<C-r>a<cr>N
The only downside is that this will overwrite your last visual selection, so if you use gv a lot, this isn't the best solution. It also overwrites the a register, although you could pick a different one if you want.

Spellchecking in Vim - tell Vim that this is okey

Yes, that title is the best I could come up with :-)
I have a text, and when activating spellchecking naturally a lot of words come out highlighted. Like emails, adresses, names and so on. How to tell Vim that some word is okey, without adding it to the wordlist.
Meaning, just, while editing this document I don't want to see my name highlighted.
Try: zG
:help internal-wordlist
From http://vimdoc.sourceforge.net/htmldoc/spell.html
zg Add word under the cursor as a good word to the first
name in 'spellfile'. A count may precede the command
to indicate the entry in 'spellfile' to be used. A
count of two uses the second entry.
In Visual mode the selected characters are added as a
word (including white space!).
When the cursor is on text that is marked as badly
spelled then the marked text is used.
Otherwise the word under the cursor, separated by
non-word characters, is used.
If the word is explicitly marked as bad word in
another spell file the result is unpredictable.
I have my Vim config in a git repository, which is useful for several things; For example, you could alias your vim to a small script that invokes Vim normally, but after Vim finishes checks if the ~/.vim/spell directory has any modifications and if so, asks you if you want to keep or discard them. If you want to keep them, it could automatically commit everything in spell and otherwise reset everything in there. So you usually wont be bothered by that script unless you actually do use the spellchecker.
The only drawback would be that you couldn't both make persistent and volatile additions to the dictionary in one session.

Go to the next same word in Vim without using search

Is there a shortcut in Vim for going to the next word which is the same as the word cursor is on? It should work without typing the word with search command /.
* goes to the next matching word and # goes to the previous matching word. * is so useful it's sometimes called the super star.
* and # are your friends ( forward and backward directions respectively )
While all the answers here are correct, I thought it may be useful to provide a little more info.
What * actually does is perform a forward search for \<word-under-the-cursor\>. Because this is just a search operation, you can then navigate forwards and backwards to the next occurrences using n and N. This also means your previous search is lost. # is exactly the same as * except it performs a reverse search.
The \< and \> in the search string are word boundaries in vim's regex language which is what makes this work so nicely. It's also important to note that what is considered a "word" is determined by the iskeyword option. See :help word for more information.
To search the current word under the cursor use '*'.
to search backwards for current cursor word use '#'
gd and its variants are also very handy. It works slightly differently from *, in that it searches for the local declaration of the word under the cursor (or global declaration if none local exists). You can then search through the results with n like you can with superstar, and it will skip comments. It can be helpful when you're editing code.
:help gd

Vim Spell option to ignore source code identifiers containint underscore, numbers, etc

Is there any option in vim spell checker to ignore words containing underscore, multiple uppercase letters, minus, numbers in a plain text file. I could not find anything in the manuals (7.2) or Google search.
You can use the syntax command with the #NoSpell cluster:
syn match myExCapitalWords +\<\w*[_0-9A-Z-]\w*\>+ contains=#NoSpell
You may want to look at :help iskeyword variable which defines what 'word' is.
You can try using zg when on the bad word - this will add the word to the allowed word list, which should then stop it being highlight in the future.
Otherwise, I'm not sure whether this is possible using the standard functionality - syntax files can specify regions that should or should not be spell checked but this will not help you, as you are including 'bad words' in comments, which would be spell checked in most cases.

Resources