11

Right now, this is a task I find a lot easier in something like gedit, because I can just replace "\n" (the line break) with "</li>\n<li>" and then I have a list.

One of the few little things I don't seem to be able to in Emacs quickly, but something I use a lot.

7

The most direct way of doing this would be to

  • Mark the region of text lines to become a list
  • Press M-% (query-replace)
  • Type C-q C-j RET </li> C-q C-j <li> RET (C-q C-j inserts a quoted newline character)
  • Press ! to replace all occurrences
  • i had to change around the opening and closing tags, but this works, and i think i can make myself remember C-q C-j. – mattl Sep 24 '14 at 5:55
10

Alternatively to query replace you can go with multiple-cursors:

enter image description here

Also consider using something like this:

(defun wrap-html-tag (tagName)
  "Add a tag to beginning and ending of current word or text selection."
  (interactive "sEnter tag name: ")
  (let (p1 p2 inputText)
    (if (use-region-p)
        (progn
          (setq p1 (region-beginning) )
          (setq p2 (region-end) )
          )
      (let ((bds (bounds-of-thing-at-point 'symbol)))
        (setq p1 (car bds) )
        (setq p2 (cdr bds) ) ) )

    (goto-char p2)
    (insert "</" tagName ">")
    (goto-char p1)
    (insert "<" tagName ">")
    ))

source

  • If you find yourself doing this sort of editing on a regular basis then I highly recommend the multiple-cursors package. Sometimes I wonder how I ever got along without it. – nispio Sep 24 '14 at 0:02
  • @welldan97 How did you create that GIF? Just curious… – lunaryorn Sep 24 '14 at 13:02
  • @lunaryorn, via LICEcap for OS X. I've heard Quicktime can be better, though I didn't try yet. – welldan97 Sep 24 '14 at 22:48
9

You can use query-replace-regexp (C-M-%). Replace ^\(.*\)$ with <li>\1</li>.

1

For a full-featured solution, convert your text into an org-mode list and export to HTML. Prefix lines with '* ', active org-mode, and run org-html-export-to-html.

  • "You really shouldn't be generating HTML using text manipulations"--I can think of many reasons one might want to do that (you can't always control your input). – shosti Sep 23 '14 at 23:21
  • Prefixing input lines with an '* ' isn't asking too much, is it? – artagnon Sep 23 '14 at 23:23
  • I believe org-mode export is excellent for an automated solution, but I don't think discouraging basic text manipulation for html generation is helpful. – dgtized Sep 23 '14 at 23:24
  • If you want to add that as an answer (prefix all lines with '*', activate org-mode, then export to HTML) then that's fine, but I don't think it makes sense to scold people for wanting "quick-and-dirty" HTML generation. – shosti Sep 23 '14 at 23:25
  • Fair enough; I've reworded it. – artagnon Sep 23 '14 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.