How to change the color of highlighted misspelled word?
Asked Answered
D

3

9

In the theme I'm using for vim, the strings are shown in red color but the problem is I have spellcheck on and the misspelled words are also shown in red color.

This makes it hard to see what is the mistake until you go to that word and delete any character.

I want to make the highlightation of the misspelled word in somewhat lighter then it currently. Say #ff2929.

                                                       You can't see what is the misspelled word

Depart answered 1/7, 2012 at 3:57 Comment(2)
A long time ago when I posted this question, I found solarized colorscheme for my shell.Depart
I use solarized myself, try highlight Pmenu ctermbg=darkred ctermfg=white guibg=black gui=bold see if that fixed the problem. Of course, set the background color according to hereEmbroider
M
15

You can use the hi (short for :help highlight) command in your ~/.vimrc. The general structure is:

hi SpellBad    ctermfg=015      ctermbg=000     cterm=none      guifg=#FFFFFF   guibg=#000000   gui=none

The cterm is for terminal vim and the gui is for gVim. The fg stands for foreground and is the color of the letters and the bg stands for background and is the color behind the letters.

Terminal colors can be 0-15 for standard terminal colors (8 normal and 8 bright) or 0-255 for terms supporting 256 colors, like xterm-256colors. The gui colors are in hexadecimal format. xterm-color-table is a useful reference for both 256 and hexadecimal colors. The final option can be used to specify bold, italic, or none (neither).

In your case, it might be simplest to set the foreground to black to make the letters stand out. First, find a word that's mispelled with :set spell and then typing asdflkjasldf or something. Then type :hi SpellBad ctermfg=000 guifg=#000 and see if that's a solution you like. If not, use the xterm-color-table or another color reference to find a color you do like.

Morra answered 17/7, 2012 at 5:13 Comment(2)
Worth noting that this doesn't fully explore the options available; :help highlight has more, including fully arbitrary escape codes.Participation
The above example should use full 6-digit hex. I found that the above only worked when, for example, I did something like the following:Entourage
E
6

Try this:

 :hi SpellBad guibg=#ff2929 ctermbg=224
  • guibg is for GUI
  • ctermbg is for TERM
Elisa answered 1/7, 2012 at 4:2 Comment(1)
I'm using vi in gnome-terminal, nothing is working when I'm doing :hi SpellBad guibg=#ff2929 and when I append ctermbg=224 I don't think spell-check works (no word is highlighted, even misspelled!)Depart
E
0

I found the following to halfway work for a more complex example involving colorscheme, but it is sensitive to the order of .vimrc commands. I tested with Cygwin/mintty and Git Bash, vim 8.0, with similar results. I edited a markdown file with "misspelled" words in headings and paragraphs, so an additional factor is the auto-formatting that vim is doing for markdown. If the .vimrc order is spell, colorscheme, and then hi (trying to use white text on red background), the result for misspelled words is white foreground on black background (image below), regardless of whether in markdown heading or paragraph. This is OK but I'd prefer to have the background for misspelled words be more eye-catching, which is why I specified red background.

colorscheme before hi

However, if the order is spell, hi, and colorscheme, the result is OK in paragraphs but undesired pink on red in headers (image below). This is actually the original behavior without hi, which makes sense because the colorscheme is probably stepping on the hi settings. Based on other testing, the relative position of hi and colorscheme is what is important.

colorschme after hi

I think I'm going to go with the first option because at least the highlights seem to be in all content, but it would be nice if the red background is used. The following is my .vimrc lines for the first case. Any guidance to fix this would be appreciated.

" Turn on spell-checker
set spell

" Color scheme
" To pick from available list do:
" :colorscheme _space_ Tab
" Reasonable options seem to be:  koehler, murphy, elford
colorscheme koehler

" Using the colorscheme with spellchecking results in highlights with
" pink text on red background, which is hard to read, so change the highlight color.
" Color table:  https://github.com/guns/xterm-color-table.vim
" Use white text on red background for misspelled words.
hi SpellBad ctermfg=015 ctermbg=009 cterm=bold guibg=#ff0000 guifg=#000000 gui=bold
Entourage answered 19/4, 2020 at 7:55 Comment(1)
You can use an autocommand on the ColorScheme event to (do anything, but also to) change highlights when the colorscheme changes. This has the advantage that you can change some colorschemes one way and some another.Martica

© 2022 - 2024 — McMap. All rights reserved.