Skip to content

Instantly share code, notes, and snippets.

@BuonOmo
Last active November 23, 2021 10:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BuonOmo/aec340bf01b9b8408897bd8203f2d74e to your computer and use it in GitHub Desktop.
Save BuonOmo/aec340bf01b9b8408897bd8203f2d74e to your computer and use it in GitHub Desktop.
Git pre-commit hook that detects non-breaking spaces
#!/usr/bin/env zsh
# Look for non-breaking spaces hidden in code, refuse
# to commit if there are some. This hook depends on the
# great ripgrep (https://github.com/BurntSushi/ripgrep).
# You can use bash if you prefere it over zsh, and ag
# over rg.
! rg --colors 'match:bg:yellow' -C 1 ' ' $(git ls-files) && exit 0
cat << TEXT
⚠️ There is a non breaking space in your code, see notice above.
You should remove it before committing, or you can ignore
this using \`git commit --no-verify\`.
To replace every non-breaking spaces in your code, you can copy paste
this snippet:
rg --files-with-matches  ' ' \$(git ls-files) | xargs sed -i '' 's/ / /'
TEXT
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment