Removing 'smart' quotes from a file, on the command-line
This post's featured URL for sharing metadata is https://www.jvt.me/img/profile.jpg.
I dislike smart quotes. They are harder to write, personally don't provide me any value, and can ruin someone's day when interacting with technical documentation.
Let's say we take the code snippet:
git commit -m "Publish blog post"
If you convert this to smart quotes, you get the following change:
-git commit -m "Publish blog post"
+git commit -m “Publish blog post“
This is done in a few places - I've had it happen to me in Slack, but it also persists across things like Google Docs, and other platforms, as well as on folks' blog posts.
The problem here is that if you copy-paste this command, you'll end up with:
$ git commit -m “Publish blog post“
error: pathspec 'blog' did not match any file(s) known to git
error: pathspec 'post“' did not match any file(s) known to git
Notice how the quotes are not interpreted as quotes, but a raw character?
It can also result in partially executed commands which could be destructive.
So how do we remove them? A straightforward solution that works for Linux, or for Mac, is the following shell script:
# Linux
sed -i -E "s/‘|’/'/g;s/“|”/\"/g" /path/to/file
# Mac
sed -I '' -E "s/‘|’/'/g;s/“|”/\"/g" /path/to/file
I've not yet written a POSIX-compliant solution, but will do at some point.