13

I'm copying text from either Google Chrome or PDFs, and pasting into Emacs.

The original text has smart quotes. I don't want smart quotes in the output.

Is there a way, either on the Copying side or on the Pasting side, to automatically strip out the smart quotes?

  • 2
  • The same problem occurs when copying from various text editors on OS X when the "use smart quotes" system preference is enabled. Specifically, I encountered this when copying from Evernote to a web form, which then stripped all the "smart" quote marks and apostrophes completely. The accepted answer provided a neat intermediate step to solve this. – TheBamf Aug 12 '15 at 9:44
14

How about:

(defun replace-smart-quotes (beg end)
  "Replace 'smart quotes' in buffer or region with ascii quotes."
  (interactive "r")
  (format-replace-strings '(("\x201C" . "\"")
                            ("\x201D" . "\"")
                            ("\x2018" . "'")
                            ("\x2019" . "'"))
                          nil beg end))

Put that in your ~/.emacs and you should be able to use M-x replace-smart-quotes to fix all quotes in the current buffer or selected region.

To avoid restarting Emacs for the ~/.emacs change to take effect, move your cursor to the end of the defun with M-C-e and evaluate it C-x C-e.

Update re comment:

To automatically do this when yanking (pasting), you could do something like the following:

(defun yank-and-replace-smart-quotes ()
  "Yank (paste) and replace smart quotes from the source with ascii quotes."
  (interactive)
  (yank)
  (replace-smart-quotes (mark) (point)))

If you then want to do that when you hit C-y, you can bind it using:

(global-set-key (kbd "C-y") 'yank-and-replace-smart-quotes)

It's probably a better idea to use another key however (maybe C-c y) as this will use some of the default yank functionality.

| improve this answer | |
  • Thanks. But what about a function that will strip the quotes out during copy/paste? Kind of like "Paste and Match Formatting" with stripping out smart quotes. – incandescentman Jun 12 '13 at 8:15
  • 2
    You can do that too, I've added an example. Don't forget to accept the answer if it solves your problem. – Tom Jun 13 '13 at 23:45

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.