As you know, functions such as replace-regexp-in-string and replace-match have an argument LITERAL, meaning ...

If optional ... arg LITERAL is non-nil, insert NEWTEXT literally.
Otherwise treat ‘\’ as special ...

My intent is to replace all occurrences of a single-quote with a back-slash followed by a single-quote. However, the output does not evaluate that way:

 (replace-regexp-in-string "'" "\'" "a'b'c" nil t)


 (replace-regexp-in-string "'" "\\'" "a'b'c" nil t)


The result I need, by hook or by crook, is:


  • @npostavs How is that a helpful comment? Anyway, no, I'm not confused, but I do need to generate the text string as I specified. May 16, 2018 at 20:16
  • 1
    Backslash will be escaped in displayed strings. Try inserting the return value of this function to see that it does what you want it to.
    – user12563
    May 16, 2018 at 20:22
  • 1
    @user1404316 okay, let me rephrase it then. I'm confused by your question as to whether you want a string composed of a backslash apostrophe etc, or if you want the string represented by the Lisp string literal "a\'b\'c"
    – npostavs
    May 16, 2018 at 20:40
  • @DoMiNeLa10 You're quite right! If you post your comment as answer, I can accept it and mark the question closed. Thanks. May 16, 2018 at 20:44

1 Answer 1


Elisp displays strings in a readable format, where characters have to be escaped, and in this case it displays \ as \\, because it has to be escaped.

Try inserting the return value like this to see that it's doing what you want it to:

(insert (replace-regexp-in-string "'" "\\'" "a'b'c" nil t))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.