7

The Vim spell checker is code-aware; it uses the syntax file to disable spell checking for the parts where it usually doesn't make any sense.

However, on occasion I do want to spell-check this. Exampels include markdown code blocks, HTML attributes, variable/function names, etc.

Note that I'm *not looking for a filetype-specific solution, but rather something generic that tells Vim to "just spell check it all".


Note: this question previously came up on SO, but only has a filetype-specific workaround. Also related is How can I check spelling in HTML attributes?; which also a file-type specific workaround.

  • 3
    The :syn off mentioned in the linked answer isn't filetype-specific, and looks like the easiest and most straightforward way to do this. – Ingo Karkat Feb 23 '15 at 8:46
  • @IngoKarkat Seeing as Carpetsmoker wrote that answer, I'm guessing it's not sufficient for his needs. – Rich Feb 23 '15 at 10:58
  • Yeah, it's what I use now on occasion, but it has the obvious side-effect of disabling all syntax highlighting (which is why I don't use it that often) ... Perhaps it's the best possible answer, both perhaps there's also a better way (so I thought it would be useful to ask again over here). – Martin Tournoij Feb 23 '15 at 11:00
3

If you want this to be something you can turn on and off (as you implied by writing "on occasion") I think you're going to need a script that can remove all the @Spell and @NoSpell clusters from the existing loaded syntax items. (See :help spell-syntax.)

One way that you might be able to implement this would be to use :redir to redirect the output from a call to :syntax to a file, and then parse that file resetting all the syntax items within, but removing references to @Spell and @NoSpell clusters. It's also possible to redirect the output into a variable.

If you're happy to enable spell checking everywhere permanently, then it would be easier to write a script to remove the clusters from the actual installed syntax files.

If you want a cleaner solution though, I think you're going to have to edit Vim itself.

0

I created bash script fixing syntax files. IT IS NOT PERFECT BUT IT IS GOOD. It can be reversed by running it again. It adds contains=@Spell to syn match and syn region definitions in all files in given directory.

To use it:

  1. Save the script as fix_syntax_files.sh
  2. Give it permissions
  3. Change path at the bottom of the script to one corresponding to your vim plugins location
  4. Run the script
  5. (OPTIONAL) Run script again to revert the changes

The script makes backup of all files before modification so you can assume it is safe to run it. I anyway do not take any responsibility for potential problems caused by the script.

Edit: You can leave feedback to the script in the following repository: https://github.com/dominikduda/config_files/blob/master/bin/fix_vim_syntax_files.sh

#!/bin/bash

function fix_file {
  sed -i -e '/exe/! {s/contains=/contains=@Spell,/g}' $1
  sed -i -e 's/contains=@Spell,ALL/contains=ALL/g' $1
  sed -i -e 's/contains=@Spell,ALLBUT/contains=ALLBUT/g' $1
  sed -i -e 's/contains=@Spell,TOP/contains=TOP/g' $1
  sed -i -e 's/contains=@Spell,CONTAINED/contains=CONTAINED/g' $1
  sed -i -e 's/contains=@Spell,NONE/contains=@Spell/g' $1
  sed -i -e '/^ *syn match/ {/contains=/! s/$/ contains=@Spell/g}' $1
  sed -i -e '/^ *syn region/ {/contains=/! s/$/ contains=@Spell/g}' $1
  return 0
}

function revert_file {
  mv "$1/$2.spellfix-backup" "$1/$2"
  return 0
}

function fix_recursively_in_catalog {
  syntax_catalogs_paths="$(find $1 -type d ! -name '*.*' -not -path '*git*' -print)"
  syntax_catalogs_count="$(echo "${syntax_catalogs_paths}" | wc -l)"

  echo "${syntax_catalogs_count} syntax catalogs found and will be scanned for files"

  echo "${syntax_catalogs_paths}" | while read -r catalog_path ; do
      echo "    Scanning $catalog_path"
      ls -p "${catalog_path}" | grep -v / | grep -v .spellfix-backup | grep .vim | while read -r file_name ; do
          cp "${catalog_path}/${file_name}" "${catalog_path}/${file_name}.spellfix-backup"
          fix_file "${catalog_path}/${file_name}"
          echo "        Fixing ${file_name} (backup created as ${file_name}.spellfix-backup)"
      done
  done
  echo 'Fix done.'
  echo 'Remember to REVERT FIX before updating vim plugins'
  return 0
}

function revert_recursively_in_catalog {
  syntax_catalogs_paths="$(find $1 -type d ! -name '*.*' -not -path '*git*' -print)"
  syntax_catalogs_count="$(echo "${syntax_catalogs_paths}" | wc -l)"

  echo "${syntax_catalogs_count} syntax catalogs found and will be scanned for spellfix-backup files"

  echo "${syntax_catalogs_paths}" | while read -r catalog_path ; do
      echo "    Scanning $catalog_path"
      ls -p "${catalog_path}" | grep -v / | grep -v .spellfix-backup | grep .vim | while read -r file_name ; do
          revert_file "${catalog_path}" "${file_name}"
          echo "        Reverting ${file_name} (from file ${file_name}.spellfix-backup)"
      done
  done
  echo 'Revert done.'
  echo 'Remember to FIX AGAIN after plugins update (or set it as a post update hook)'
  return 0
}

function main {
  syntax_catalogs_paths="$(find $1 -type d ! -name '*.*' -not -path '*git*' -print)"
  while read -r catalog_path ; do
      if ls -p "${catalog_path}" | grep -v / | grep .spellfix-backup; then
        echo ".spellfix-backup files found, reverting fix!"
        echo "--------------------------------------------"
        revert_recursively_in_catalog $1
        return 0
      fi
  done < <(echo "${syntax_catalogs_paths}")
  echo ".spellfix-backup files NOT found, fixing!"
  echo "-----------------------------------------"
  fix_recursively_in_catalog $1
}

main ~/PATH/TO/VIM/PLUGINS/
  • Link seems dead? You have fix_syntax_files.sh and fix_vim_syntax_files.sh here, which seem the same? Not sure which one is the correct one to link to? – Martin Tournoij May 25 '17 at 23:00
  • Sorry I reoriented my config_files repo. Link is updated. – dominikduda May 26 '17 at 6:55

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.