Emacs: convert items on separate lines to a comma-separated list
emacs
Solution
M-q would replace linebreaks with spaces (in reasonably short lists of short words) but won't add quotes and commas. Or, maybe M-^ many times, until you have them all on the same line. Other than that - nothing built in comes to mind.
Obviously, a keyboard macro is a good candidate for this.
But a faster way, that doesn't create many undo steps would be something along these lines:
(defun lines-to-cslist (start end &optional arg)
(interactive "r\nP")
(let ((insertion
(mapconcat
(lambda (x) (format "\"%s\"" x))
(split-string (buffer-substring start end)) ", ")))
(delete-region start end)
(insert insertion)
(when arg (forward-char (length insertion)))))
Problem
I often paste items separated by newlines or line feeds into an Emacs buffer resulting in each item residing on a different line like this: ``` one two three four ``` Very often I actually want a list of comma-separated values like this: ``` "one", "two", "three", "four" ``` It would be great to be able to do a one-touch conversion from lines to list. I imagine I can convert this using a regex, but it seems like the kind of commonly used operation that might already have a built-in Emacs function. Can anybody suggest one?