Sean Perry
Sean Perry

Reputation: 3886

Replaces lines in buffer with each line wrapped in a block of text?

There is a buffer with the following contents.

foo
bar baz bat
more text
Lorem Ipsum fido

The goal is to turn this listing into a HTML table with the following block wrapping each line of the buffer.

<tr>
<td style="text-align: center;">
??Text Here??
</td>
<td style="text-align: center;">
8</td>
<td style="text-align: center;">
&#160;</td>
</tr>

With ??Text Here replaced with a line from the buffer.

Thoughts?

Upvotes: 4

Views: 73

Answers (1)

Tobias
Tobias

Reputation: 5198

If the lines are not too long the following works (note the \&-tick that expands to the full match as it is described in the help of query-replace-regexp):

  1. Set the cursor to the beginning of the first line.
  2. Press M-:
  3. Paste the following text into the minibuffer and press enter:
(query-replace-regexp "^.*$" "<tr>
<td style=\"text-align: center;\">
\\&
</td>
<td style=\"text-align: center;\">
8</td>
<td style=\"text-align: center;\">
&#160;</td>
</tr>")
  1. Press ! to replace all lines.

Alternatively you can also:

  1. Set the cursor to the beginning of the first line.
  2. Press C-M-% to interactively invoke query-replace-regexp
  3. Paste the source regexp ^.*$ into the minibuffer and press enter
  4. following into the minibuffer:
<tr>
<td style="text-align: center;">
\&
</td>
<td style="text-align: center;">
8</td>
<td style="text-align: center;">
&#160;</td>
</tr>

and press enter and ! to replace all matches.

Upvotes: 2

Related Questions