11

Suppose I have the following list. I'd like to convert it to a checklist.

Lec 1 |         1:20:36
Lec 2 |         1:10:32
Lec 3 |         1:08:33
Lec 4 |         1:20:33
Lec 5 |         1:16:50
Lec 6 |         1:08:49
Lec 7 |         1:17:40
Lec 8 |         1:19:47
Lec 9 |         1:21:22
Lec 10 |        1:23:52
Lec 11 |        1:23:45
Lec 12 |        1:25:32
Lec 13 |        1:19:06
Lec 14 |        1:14:28
Lec 15 |        1:11:01
Lec 16 |        1:24:07
Lec 17 |        1:24:34
Lec 18 |        1:17:17
Lec 19 |        1:14:59
Lec 22 |        1:15:08
Lec 23 |        1:16:48
Lec 24 |        1:24:47
Lec 25 |        1:25:21

How to do it?

(I did it in using kbd-macro. I wonder is there an org command to do it?)

  • There are many ways and the quickness is subjective. I would use multiple cursors or query-search-replace. By converting to check list, you mean to simply prepend the lines with [ ], correct? – Kaushal Modi Dec 21 '14 at 14:00
  • Yes. Can you briefly show how to use multiple cursors or query-search-replace? – Nick Dec 21 '14 at 14:23
  • Here's a detailed explanation of how to use multiple-cursors for doing search-replace. That would apply to this case too. – Kaushal Modi Dec 21 '14 at 15:05
  • Quick web searches bring up the links to multiple-cursors and the manual page on Query Replace. A lot of this stuff is really well documented, and is just a web search away. – Dan Dec 21 '14 at 15:09
  • Thank you. It seems to be advanced and a little complex. I need to get familiar with those command/tools. – Nick Dec 21 '14 at 15:15
21

Simplest way I could think of:

  1. Select the list.
  2. Move the point to the first column.
  3. C-x r t- [ ]RET

You are done.

  • 1
    Wow! The rectangle editing is so straight forward yet powerful! Thank you! – Nick Dec 22 '14 at 0:11
  • Such awesome. Very useful for conversion of checklists. – OrgAddict Sep 27 '17 at 8:19
11

First, some semantics for clarity. In org-mode, a plain list is either ordered or unordered, starting with either a -, +, or * (for unordered), or a number followed by either a . or a ) (for ordered). So: the "list" you describe in your example is not yet an org-mode list, because it doesn't start with any of these bullets.

Second, I presume by "checklist" you mean the checkboxes that org-mode uses in its plain lists, as in:

- [X] A checked list item
- [ ] An unchecked list item

Here's a very simple function that will convert all lines in the selected region to an unordered list with checkboxes (not extensively tested, but works on your example):

(defun org-convert-lines-to-checklist (beg end)
  "Convert all plain lines in region to a plain list with
checkboxes."
  (interactive "r")
  (save-excursion
    (goto-char beg)
    (dotimes (_ (- (line-number-at-pos end) (line-number-at-pos beg)))
      (insert "- [ ] ")
      (indent-according-to-mode)
      (forward-line 1))))
7

Below is another fun way to transform text into an org-mode checklist.

Use Org-mode Code Blocks to Convert Text into List of Checkboxes

Note: To generate the results use C-c C-c while the cursor is within a code block.
Then answer yes when prompted.

  1. Wrap your list inside a named dynamic block

    #+NAME: my-list-block  
    #+BEGIN:  
    Lec 1 |         1:20:36'  
    Lec 2 |         1:10:32  
    Lec 3 |         1:08:33  
    Lec 4 |         1:20:33  
         ... More ...  
    Lec 24 |        1:24:47  
    Lec 25 |        1:25:21  
    #+END:  
    
  2. Write an org-mode code block in your favorite programming language.

    Example 1 - Using an elisp Code Block

    #+name: list-into-checklist-elisp
    #+header: :results list raw replace output 
    #+header: :var data=my-list-block()
    #+begin_src elisp
      (dolist (x (split-string data "\n"))
            (princ (format "[ ] %s\n" x)))
    #+end_src
    
    #+RESULTS: list-into-checklist-elisp
    - [ ] Lec 1 |         1:20:36
    - [ ] Lec 2 |         1:10:32
    - [ ] Lec 3 |         1:08:33
    - [ ] Lec 4 |         1:20:33
    - [ ]       ... More ...
    - [ ] Lec 24 |        1:24:47
    - [ ] Lec 25 |        1:25:21
    

    Example 2 - Using a perl Code Block

    #+name: list-into-checklist-perl
    #+header: :results list raw replace output
    #+header: :var data=my-list-block()
    #+begin_src perl
      map { printf qq([ ] %s\n), $_ } split(/\n/, $data); 
    #+end_src
    
    #+RESULTS: list-into-checklist-perl
    - [ ] Lec 1 |         1:20:36
    - [ ] Lec 2 |         1:10:32
    - [ ] Lec 3 |         1:08:33
    - [ ] Lec 4 |         1:20:33
    - [ ]       ... More ...
    - [ ] Lec 24 |        1:24:47
    - [ ] Lec 25 |        1:25:21
    

    Example 3 - Using a bash Code Block

    #+name: list-into-checklist-bash
    #+header: :results list raw replace output
    #+header: :shebang #!/usr/bin/env bash
    #+header: :var data=my-list-block()
    #+begin_src sh
      while IFS="\n" read -ra ADDR; do
            for i in "${ADDR[@]}"; do
                echo "[X] $i"
            done
       done <<< "$data"
    #+end_src
    
    #+RESULTS: list-into-checklist-bash
    - [X] Lec 1 |         1:20:36
    - [X] Lec 2 |         1:10:32
    - [X] Lec 3 |         1:08:33
    - [X] Lec 4 |         1:20:33
    - [X]       ... More ...
    - [X] Lec 24 |        1:24:47
    - [X] Lec 25 |        1:25:21
    

    Example 4 - Using a python Code Block

    #+name: list-into-checklist-python
    #+header: :results list raw replace output
    #+header: :var data=my-list-block()
    #+Begin_src python
      l = ["[ ] {x}".format(x=row) for row in data.splitlines()]
      for i in l: print i
    #+end_src 
    
    #+RESULTS: list-into-checklist-python
    - [ ] Lec 1 |         1:20:36
    - [ ] Lec 2 |         1:10:32
    - [ ] Lec 3 |         1:08:33
    - [ ] Lec 4 |         1:20:33
    - [ ]       ... More ...
    - [ ] Lec 24 |        1:24:47
    - [ ] Lec 25 |        1:25:21
    

    Example 5 - Using a ruby Code Block

    #+name: list-into-checklist-ruby
    #+header: :results list raw replace output
    #+header: :var data=my-list-block()
    #+Begin_src ruby
      for l in  data.split("\n")
        puts "[ ] #{l}"
      end
    #+end_src 
    
    #+RESULTS: list-into-checklist-ruby
    - [ ] Lec 1 |         1:20:36
    - [ ] Lec 2 |         1:10:32
    - [ ] Lec 3 |         1:08:33
    - [ ] Lec 4 |         1:20:33
    - [ ]       ... More ...
    - [ ] Lec 24 |        1:24:47
    - [ ] Lec 25 |        1:25:21
    

Thanks for asking your question!

Hope that helped!

Note: This code was tested using the following versions of emacs and org-mode.

GNU Emacs 24.4.1 (x86_64-apple-darwin14.0.0, NS apple-appkit-1343.14)
Org-mode version 8.2.10 (8.2.10-29-g89a0ac-elpa)
6

Using search and replace:

M-%Lec Enter - [ ] Lec Enter

Note that there are spaces around the checkbox, although they don't show up well here.

  • This also works very well. Sorry I can only mark one as the answer, so I can only vote up. Thank you very much. – Nick Dec 22 '14 at 0:12
3

In Evil mode or Spacemacs you can do this, assuming you have not changed the default key-bindings:

  1. In Normal state (equivalent to Vim's Normal mode), move the cursor to the beginning of the first line in your list.

  2. Press Ctrl+v.

  3. Press j once for each remaining line in your list. (Alternatively, type the number of remaining lines in your list, followed by the j key. E.g. for your example: 24j.)

  4. Press Shift+i.

  5. Type - [ ].

  6. Press Esc.

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.