├── README.markdown └── plugin └── checkbox.vim /README.markdown: -------------------------------------------------------------------------------- 1 | 2 | 3 | Vim Checkbox 4 | ============ 5 | 6 | 7 | Description 8 | ----------- 9 | 10 | Simple plugin that toggles text checkboxes in Vim. Works great if you're using 11 | a markdown file for notes and todo lists. 12 | 13 | 14 | Installation 15 | ------------ 16 | 17 | Just copy the script into your plugin folder, e.g. `~/.vim/plugin/`. If you're 18 | using pathogen, just clone this repository in `~/.vim/bundle`. 19 | 20 | 21 | Usage 22 | ----- 23 | 24 | Press `tt` to toggle the (first) checkbox on the current 25 | line, if any. That means, `[ ]` will be replaced with `[x]` and `[x]` with 26 | `[ ]`. If you want more or different checkbox states, you can override the 27 | contents of `g:checkbox_states` with an array of characters, which the plugin 28 | will cycle through. The default is: 29 | 30 | let g:checkbox_states = [' ', 'x'] 31 | 32 | When there's no checkbox on the current line, `tt` will insert one 33 | at the pattern defined in `g:insert_checkbox`. The new checkbox's state will 34 | be the first element of `g:checkbox_states`. The default for `g:insert_checkbox` 35 | is `'\<'`, which will insert the checkbox in front of the first word of the 36 | line (not necessarily at the beginning of the line, `'^'`), which is 37 | particularly useful when working in markdown-formatted lists. Other useful 38 | patterns would be `'^'` (insert at the very beginning of the line) and `'$'` (end 39 | of line). When inserting a new checkbox, `g:insert_checkbox_prefix` and 40 | `g:insert_checkbox_suffix` are prepended/appended, respectively. This is mostly 41 | useful for adding a space behind or in front of the checkbox: 42 | 43 | let g:insert_checkbox_prefix = '' 44 | let g:insert_checkbox_suffix = ' ' 45 | 46 | Inserting a checkbox can be disabled by setting `g:insert_checkbox` to an 47 | empty string (`''`). 48 | -------------------------------------------------------------------------------- /plugin/checkbox.vim: -------------------------------------------------------------------------------- 1 | " **************************************************************************** 2 | " File: checkbox.vim 3 | " Author: Jonas Kramer 4 | " Version: 0.1 5 | " Last Modified: 2018-05-07 6 | " Copyright: Copyright (C) 2010-2018 by Jonas Kramer. Published under the 7 | " terms of the Artistic License 2.0. 8 | " **************************************************************************** 9 | " Installation: Copy this script into your plugin folder. 10 | " Usage: Press "tt" to toggle the (first) checkbox on the current 11 | " line, if any. That means, "[ ]" will be replaced with "[x]" and "[x]" with 12 | " "[ ]". If you want more or different checkbox states, you can override the 13 | " contents of g:checkbox_states with an array of characters, which the plugin 14 | " will cycle through. The default is: 15 | " 16 | " let g:checkbox_states = [' ', 'x'] 17 | " 18 | " When there's no checkbox on the current line, "tt" will insert one 19 | " at the pattern defined in g:insert_checkbox. The new checkbox's state will 20 | " be the first element of g:checkbox_states. The default for g:insert_checkbox 21 | " is '\<', which will insert the checkbox in front of the first word of the 22 | " line (not necessarily at the beginning of the line, '^'), which is 23 | " particularly useful when working in markdown-formatted lists. The pattern 24 | " can be overridden. Other useful patterns would be '^' (insert at the very 25 | " beginning of the line) and '$' (end of line). When inserting a new checkbox, 26 | " g:insert_checkbox_prefix and g:insert_checkbox_suffix are 27 | " prepended/appended, respectively. This is mostly useful for adding a space 28 | " behind the checkbox, which is the default: 29 | " 30 | " let g:insert_checkbox_prefix = '' 31 | " let g:insert_checkbox_suffix = ' ' 32 | " 33 | " Inserting a checkbox can be disabled by setting g:insert_checkbox to an 34 | " empty string (''). 35 | " **************************************************************************** 36 | 37 | if exists('g:loaded_checkbox') 38 | finish 39 | endif 40 | 41 | if !exists('g:checkbox_states') 42 | let g:checkbox_states = [' ', 'x'] 43 | endif 44 | 45 | 46 | if !exists('g:insert_checkbox') 47 | "let g:insert_checkbox = '^' 48 | "let g:insert_checkbox = '$' 49 | let g:insert_checkbox = '\<' 50 | endif 51 | 52 | if !exists('g:insert_checkbox_prefix') 53 | let g:insert_checkbox_prefix = '' 54 | endif 55 | 56 | if !exists('g:insert_checkbox_suffix') 57 | let g:insert_checkbox_suffix = ' ' 58 | endif 59 | 60 | fu! checkbox#ToggleCB() 61 | let line = getline('.') 62 | 63 | if(match(line, '\[.\]') != -1) 64 | let states = copy(g:checkbox_states) 65 | call add(states, g:checkbox_states[0]) 66 | 67 | for state in states 68 | if(match(line, '\[' . state . '\]') != -1) 69 | let next_state = states[index(states, state) + 1] 70 | let line = substitute(line, '\[' . state . '\]', '[' . next_state . ']', '') 71 | break 72 | endif 73 | endfor 74 | else 75 | if g:insert_checkbox != '' 76 | let line = substitute(line, g:insert_checkbox, g:insert_checkbox_prefix . '[' . g:checkbox_states[0] . ']' . g:insert_checkbox_suffix, '') 77 | endif 78 | endif 79 | 80 | call setline('.', line) 81 | endf 82 | 83 | command! ToggleCB call checkbox#ToggleCB() 84 | 85 | map tt :call checkbox#ToggleCB() 86 | 87 | let g:loaded_checkbox = 1 88 | --------------------------------------------------------------------------------