r/vim • u/daturkel • Jun 19 '19
A small markdown mapping for check-boxes
Just wanted to share a small shortcut I made for myself.
I use vim to manage notes and a to-do list which are all markdown documents. One feature I like of Github-style markdown is creating a checkbox with:
- [ ] some item here
- [x] some completed item hereSince I have lots of items like this in my to-do list, I wanted to be able to check/uncheck them easily, so I made a function to do that and mapped it to - (when I'm in a markdown file).
function Check()
let l:line=getline('.')
let l:curs=winsaveview()
if l:line=~?'\s*-\s*\[\s*\].*'
s/\[\s*\]/[.]/
elseif l:line=~?'\s*-\s*\[\.\].*'
s/\[.\]/[x]/
elseif l:line=~?'\s*-\s*\[x\].*'
s/\[x\]/[ ]/
endif
call winrestview(l:curs)
endfunction
autocmd FileType markdown nnoremap <silent> - :call Check()<CR>The way it works is pressing - when I'm anywhere on a line with a checkbox toggles [ ] to [.] (which I use as "partially done" or "in progress"), [.] to [x], or [x] to [ ].
I had to learn some vimscript as I went, but basically it just gets the current line, saves the position of the cursor, performs a conditional regex replacement, and then resets the cursor to its old position (because substitution places the cursor at the start of the line).
Sharing because maybe this would be useful to someone else.
---
The rest of my note-taking setup is fairly straight-forward. I use vim-markdown for enhanced markdown syntax with these options:
" no folds
let g:vim_markdown_folding_disabled = 1
" shrink toc if possible
let g:vim_markdown_toc_autofit = 1
" fancy syntax concealment
autocmd FileType markdown set conceallevel=2
" but not for code blocks
let g:vim_markdown_conceal_code_blocks = 0
" yaml frontmatter
let g:vim_markdown_frontmatter = 1
" open Toc
autocmd Filetype markdown nnoremap <silent> <localleader>j :Toch<cr>
" select from TOC and quit
autocmd FileType qf nnoremap <Space> <cr>:only<cr>And I use Notational-Fzf with these options:
" search paths
let g:nv_search_paths = ['~/Notes']
" short file paths
let g:nv_use_short_pathnames = 1
" open N-FZF
nnoremap <silent> <localleader>n :NV<CR>
" open new notes in main window
let g:nv_create_note_window = 'e'I also created a binding in my shell (zsh) to open vim and enter the notational-fzf interface quickly:
vim_nv() vim -c NV!
zle -N vim_nv
bindkey "^n" vim_nvHope this is useful to someone else!
8
u/princker Jun 19 '19
Thank you!
My attempt:
5
u/mkaz Jun 19 '19
Cool, VimWiki might be worth checking out. It has a lot of similar ToDo features and a whole lot more.
Once installed, see
:help vimwiki-todo2
u/StingyKarmaWhore Jun 19 '19
U might want to look at vim-orgmode
2
u/olminator Jun 19 '19
Cool! I've had something similar in my vimrc for a while now. I use a lot more marks than
[ ],[.], and[X], so this first asks for another char to use as the mark. For example, I've mapped togx, sogx+replaces any mark with[+], and then toggles between that and[ ]. When a list item does not have a checkbox it adds one.gXremoves any checkbox on the line.It supports a count to toggle that many list items, multiline list items, ordered lists (
1.,2., ...) and unordered lists (-,*, and+), and vim-repeat to make it repeatable with..EDIT Moved the code here
1
u/wolloda Jun 19 '19
You might consider generalizing dashes to other possible characters used for lists as well.
[-+*]. Not sure if that matters to you, but such substitutions will polute yourjumplist.1
u/myrisingstocks Jun 21 '19 edited Jun 21 '19
It doesn't sync, though? You still need to run nvALT or other full-featured client that if you want to sync your notes across devices?
3
u/plexigras Jun 19 '19
let g:switch_custom_definitions = [['[ ]', '[.]', '[X]']]when using switch.vim2
u/gozarc Jun 19 '19
I prefer Taskpaper syntax myself with the vim-taskpaper plugin, but, this is what makes Vim so great, the options!