19

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?)

Dan
  • 32,584
  • 6
  • 98
  • 168
Nick
  • 4,423
  • 4
  • 24
  • 41
  • 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](http://emacs.stackexchange.com/a/243/115) 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`](https://github.com/magnars/multiple-cursors.el) and the manual page on [Query Replace](http://www.gnu.org/software/emacs/manual/html_node/emacs/Query-Replace.html). 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

7 Answers7

33

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.

wvxvw
  • 11,222
  • 2
  • 30
  • 55
20

Vanilla org-mode:

  • C-c - (org-ctrl-c-minus) - Convert selected text into a plain list
  • C-u C-c C-x C-b (org-toggle-checkbox) - Convert selected plain list into a list with checkboxes

Spacemacs:

  • , - (org-ctrl-c-minus) - Convert selected text into a plain list
  • SPC u , T c (org-toggle-checkbox) - Convert selected plain list into a list with checkboxes
Dario Ceccoli
  • 301
  • 2
  • 2
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))))
Dan
  • 32,584
  • 6
  • 98
  • 168
8

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)
Melioratus
  • 4,504
  • 1
  • 25
  • 43
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.

user2699
  • 2,181
  • 16
  • 32
  • 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 00: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.

3

For short, C-u C-c C-c on the first item of a list can turn the whole list into checklist.

Checkboxes (The Org Manual) says

C-c C-c (org-toggle-checkbox)

Toggle checkbox status or — with prefix argument — checkbox presence at point.

  • With a single prefix argument, add an empty checkbox or remove the current one.

  • With a double prefix argument, set it to ‘[-]’, which is considered to be an intermediate state.

There is a note on the single prefix argument:

C-u C-c C-c on the first item of a list with no checkbox adds checkboxes to the rest of the list.

Ynjxsjmh
  • 273
  • 2
  • 8