Removing 'smart' quotes from a file, on the command-line

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.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #command-line #mac #linux.

Also on:

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.