1

When I do this:

:set spell spelllang=en_us
:hi SpellBad cterm=underline

It underlines the spell on markdown. I just need this auto command on markdown files.

If I put these commands in my .vimrc, and reopen a new vim with a README.md it doesn't underline the misspelled word.

How do I automatically underline bad spells on .md files. I use gruvbox colorscheme if that's any relevant. But I want a simple generic answer to apply to everything.

6
  • 2
    Try an ftplugin: put the command in ~/.vim/after/ftplugin/markdown.vim
    – D. Ben Knoble
    Oct 6 at 20:12
  • I tried ftplugin, spell checking is enabled but it still doesn't underline automatically, unless I explicitly type the hi command for the buffer.
    – eguneys
    Oct 6 at 20:25
  • Are you putting it in ~/.vim/after/ftplugin and not ~/.vim/ftplugin? It's possible a color scheme is clearing the highlight group. Oct 6 at 20:40
  • It picks up the first command, because spell check is enabled, but the second is possible to be cleared by gruvbox, I don't know what to do.
    – eguneys
    Oct 6 at 20:51
  • My usual advice for customizing a colorscheme (remembering that they apply globally, AFAIK) is to use something like autocmd Colorscheme * highlight SpellBad cterm=underline (replace the * if you want to be more specific, and as always wrap in an augroup)
    – D. Ben Knoble
    Oct 7 at 1:31
2

You could try putting it in an ftplugin; putting the command in ~/.vim/after/ftplugin/markdown.vim will define your highlight group after the color scheme is loaded on startup.

However, this may break when changing color schemes because color schemes will typically call :highlight clear to clear any previously defined highlight groups before defining their own. This can cause user highlight groups defined in .vimrc to be overridden.

To remedy this, you can create an autocommand that will execute a command every time a color scheme is loaded, but you'll need to check the filetype of the current buffer.

So, replace

set spelllang=en_us
hi SpellBad cterm=underline

in your .vimrc with

augroup UnderlineSpell
    autocmd!
    autocmd ColorScheme * if &ft ==? 'markdown' 
                          \| highlight SpellBad cterm=underline
                          \| set spelllang=en_us
                       \| endif
augroup END

It's important to wrap the autocommand in an augroup to prevent defining the autocommand more than once (if you were to change color schemes, for example). autocmd! is used to clear any existing autocommands defined for the current augroup.

There's also a very informative GitHub gist about overriding color schemes in this way that would be a good read.


Also see the following help pages:

2
  • The second autocmd should not need a !; I'm not sure if it will break anything, but it's definitely not necessary (and might well be wrong). Anyway, you missed the spelling settings; you could edit and fold in my comments about ftplugins and I won't mind
    – D. Ben Knoble
    Oct 8 at 0:00
  • @D.BenKnoble Thanks, I've incorporated your suggestions :) Oct 8 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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