Emacs: Quote Lines šŸš€

By Xah Lee. Date: . Last updated: .

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

For example,

cat
dog
cow

becomes

"cat",
"dog",
"cow",

or with any other quote character:

'cat',
'dog',
'cow',

or without comma:

"cat"
"dog"
"cow"

or with any type of brackets:

(cat)
(dog)
(cow)

Here it is.

(defun xah-quote-lines (Begin End QuoteL QuoteR Sep)
  "Add quotes/brackets and separator (comma) to lines.
Act on current block or selection.

For example,

 cat
 dog
 cow

becomes

 \"cat\",
 \"dog\",
 \"cow\",

or

 (cat)
 (dog)
 (cow)

In lisp code, QuoteL QuoteR Sep are strings.

URL `http://xahlee.info/emacs/emacs/emacs_quote_lines.html'
Version: 2020-06-26 2021-09-15 2022-04-07 2022-04-13 2023-03-04"
  (interactive
   (let* (($bds (xah-get-bounds-of-block-or-region))
          ($p1 (car $bds))
          ($p2 (cdr $bds))
          ($brackets
           '(
             "\"double quote\""
             "'single quote'"
             "(paren)"
             "{brace}"
             "[square]"
             "<greater>"
             "`emacs'"
             "`markdown`"
             "~tilde~"
             "=equal="
             "ā€œcurly doubleā€"
             "ā€˜curly single’"
             "‹french angle›"
             "«french double angle»"
             "怌corner怍"
             "none"
             "other"
             )) $bktChoice $sep $sepChoice $quoteL $quoteR)
     (setq $bktChoice (completing-read "Quote to use:" $brackets))
     (setq $sepChoice (completing-read "line separator:" '("comma ," "semicolon ;" "none" "other")))
     (cond
      ((string-equal $bktChoice "none")
       (setq $quoteL "" $quoteR ""))
      ((string-equal $bktChoice "other")
       (let (($x (read-string "Enter 2 chars, for begin/end quote:")))
         (setq $quoteL (substring-no-properties $x 0 1)
               $quoteR (substring-no-properties $x 1 2))))
      (t (setq $quoteL (substring-no-properties $bktChoice 0 1)
               $quoteR (substring-no-properties $bktChoice -1))))
     (setq $sep
           (cond
            ((string-equal $sepChoice "comma ,") ",")
            ((string-equal $sepChoice "semicolon ;") ";")
            ((string-equal $sepChoice "none") "")
            ((string-equal $sepChoice "other") (read-string "Enter separator:"))
            (t $sepChoice)))
     (list $p1 $p2 $quoteL $quoteR $sep)))
  (let (($p1 Begin) ($p2 End) ($quoteL QuoteL) ($quoteR QuoteR) ($sep Sep))
    (save-excursion
      (save-restriction
        (narrow-to-region $p1 $p2)
        (goto-char (point-min))
        (catch 'EndReached
          (while t
            (skip-chars-forward "\t ")
            (insert $quoteL)
            (end-of-line)
            (insert $quoteR $sep)
            (if (eq (point) (point-max))
                (throw 'EndReached t)
              (forward-char))))))))

you need xah-get-bounds-of-block-or-region at Emacs: xah-get-thing.el