<p>Say I have a line in an emacs buffer that looks like this:</p>

<pre><code>foo -option1 value1 -option2 value2 -option3 value3 \
    -option4 value4 ...
</code></pre>

<p>I want it to look like this:</p>

<pre><code>foo -option1 value1 \
    -option2 value2 \
    -option3 value3 \
    -option4 value4 \
    ...
</code></pre>

<p>I want each option/value pair on a separate line. I also want those subsequent lines indented appropriately according to mode rather than to add a fixed amount of whitespace. I would prefer that the code work on the current block, stopping at the first non-blank line or line that does not contain an option/value pair though I could settle for it working on a selected region. </p>

<p>Anybody know of an elisp function to do this? </p>

[{'answer_id': 69032, 'author': 'Jason Dagit', 'author_id': 5113, 'author_profile': 'https://Stackoverflow.com/users/5113', 'pm_score': 1, 'selected': False, 'text': '<p>In this case I would use a macro.  You can start recording a macro with C-x (, and stop recording it with C-x ).  When you want to replay the macro type C-x e.</p>\n\n<p>In this case, I would type, C-a C-x ( C-s v a l u e C-f C-f \\ RET SPC SPC SPC SPC C-x )</p>\n\n<p>That would record a macro that searches for "value", moves forward 2, inserts a slash and newline, and finally spaces the new line over to line up.  Then you could repeat this macro a few times.</p>\n\n<p>EDIT: I just realized, your literal text may not be as easy to search as "value1".  You could also search for spaces and cycle through the hits.  For example, hitting, C-s a few times after the first match to skip over some of the matches.</p>\n\n<p>Note: Since your example is "ad-hoc" this solution will be too.  Often you use macros when you need an ad-hoc solution.  One way to make the macro apply more consistently is to put the original statement all on one line (can also be done by a macro or manually).</p>\n\n<p>EDIT: Thanks for the comment about ( versus C-(, you were right my mistake!</p>\n'}, {'answer_id': 69161, 'author': 'mike511', 'author_id': 9593, 'author_profile': 'https://Stackoverflow.com/users/9593', 'pm_score': 0, 'selected': False, 'text': '<p>Personally, I do stuff like this all the time.</p>\n\n<p>But I don\'t write a function to do it unless I\'ll be doing it\nevery day for a year.</p>\n\n<p>You can easily do it with query-replace, like this:</p>\n\n<p>m-x (query-replace " -option" "^Q^J    -option")</p>\n\n<p>I say ^Q^J as that is what you\'ll type to quote a newline and put it in\nthe string.</p>\n\n<p>Then just press \'y\' for the strings to replace, and \'n\' to skip the wierd\ncorner cases you\'d find.</p>\n\n<p>Another workhorse function is query-replace-regexp that can do\nreplacements of regular expressions.</p>\n\n<p>and also grep-query-replace, which will perform query-replace by parsing\nthe output of a grep command.  This is useful because you can search\nfor "foo" in 100 files, then do the query-replace on each occurrence\nskipping from file to file.</p>\n'}, {'answer_id': 69274, 'author': 'Allen', 'author_id': 6043, 'author_profile': 'https://Stackoverflow.com/users/6043', 'pm_score': 0, 'selected': False, 'text': '<p>Your mode may support this already. In C mode and Makefile mode, at least, M-q (fill-paragraph) will insert line continuations in the fill-column and wrap your lines.</p>\n\n<p>What mode are you editing this in?</p>\n'}, {'answer_id': 76888, 'author': 'Bryan Oakley', 'author_id': 7432, 'author_profile': 'https://Stackoverflow.com/users/7432', 'pm_score': 3, 'selected': True, 'text': '<p>Nobody had what I was looking for so I decided to dust off my elisp manual and do it myself. This seems to work well enough, though the output isn\'t precisely what I asked for. In this version the first option goes on a line by itself instead of staying on the first line like in my original question.</p>\n\n<pre class="lang-lisp prettyprint-override"><code>(defun tcl-multiline-options ()\n  "spread option/value pairs across multiple lines with continuation characters"\n  (interactive)\n  (save-excursion\n    (tcl-join-continuations)\n    (beginning-of-line)\n    (while (re-search-forward " -[^ ]+ +"  (line-end-position) t)\n      (goto-char (match-beginning 0))\n      (insert " \\\\\\n")\n      (goto-char (+(match-end 0) 3))\n      (indent-according-to-mode)\n      (forward-sexp))))\n\n(defun tcl-join-continuations ()\n  "join multiple continuation lines into a single physical line"\n  (interactive)\n  (while (progn (end-of-line) (char-equal (char-before) ?\\\\))\n    (forward-line 1))\n  (while (save-excursion (end-of-line 0) (char-equal (char-before) ?\\\\))\n    (end-of-line 0)\n    (delete-char -1)\n    (delete-char 1)\n    (fixup-whitespace)))\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/68993', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/7432/']