Emacs: Quote Lines

By Xah Lee. Date: . Last updated: .

Here's a command to convert lines to quoted lines.

For example,




or with any other quote character:


or without comma:


or with any type of brackets:


Here it is.

(defun xah-quote-lines ()
  "Change current text block's lines to quoted lines with comma or other separator char.
When there is a text selection, act on the selection, else, act on a text block separated by blank lines.

For example,






If the delimiter is any left bracket, the end delimiter is automatically the matching bracket.

URL `http://ergoemacs.org/emacs/emacs_quote_lines.html'
Version 2017-01-08"
  (let* (
           "Quote to use:" "\"" nil
           "line separator:" "," nil
         ($beginQuote $quoteToUse)
          ;; if begin quote is a bracket, set end quote to the matching one. else, same as begin quote
          (let (($syntableValue (aref (syntax-table) (string-to-char $beginQuote))))
            (if (eq (car $syntableValue ) 4) ; ; syntax table, code 4 is open paren
                (char-to-string (cdr $syntableValue))
    (if (use-region-p)
          (setq $p1 (region-beginning))
          (setq $p2 (region-end)))
        (if (re-search-backward "\n[ \t]*\n" nil "NOERROR")
            (progn (re-search-forward "\n[ \t]*\n")
                   (setq $p1 (point)))
          (setq $p1 (point)))
        (re-search-forward "\n[ \t]*\n" nil "NOERROR")
        (skip-chars-backward " \t\n" )
        (setq $p2 (point))))
        (narrow-to-region $p1 $p2)
        (goto-char (point-min))
        (skip-chars-forward "\t ")
        (insert $beginQuote)
        (goto-char (point-max))
        (insert $endQuote)
        (goto-char (point-min))
        (while (re-search-forward "\n\\([\t ]*\\)" nil "NOERROR" )
           (concat $endQuote $separator (concat "\n" (match-string 1)) $beginQuote) "FIXEDCASE" "LITERAL"))

Emacs Text Transform Under Cursor

  1. elisp wrapper + Python Ruby …
  2. Toggle Letter Case
  3. Title Case
  4. Upcase Sentences
  5. Cycle Space Hyphen Underscore
  6. Escape Quotes
  7. Quote Lines
  8. Spaces to New Lines
  9. Change Brackets/Quotes
  10. Remove Accent Marks
  11. Convert Straight/Curly Quotes
  12. Convert English/Chinese Punctuations
  13. Color Conversion (RGB, HSL, HSV)
  14. Decimal to Hexadecimal
  15. Replace Greek Letter Names to Unicode
  16. Twitterfy Text
  17. Toggle line wrap
  18. Clean White Space

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs


Emacs Lisp