-
-
Save sedm0784/dffda43bcfb4728f8e90 to your computer and use it in GitHub Desktop.
| " Auto lists: Automatically continue/end lists by adding markers if the | |
| " previous line is a list item, or removing them when they are empty | |
| function! s:auto_list() | |
| let l:preceding_line = getline(line(".") - 1) | |
| if l:preceding_line =~ '\v^\d+\.\s.' | |
| " The previous line matches any number of digits followed by a full-stop | |
| " followed by one character of whitespace followed by one more character | |
| " i.e. it is an ordered list item | |
| " Continue the list | |
| let l:list_index = matchstr(l:preceding_line, '\v^\d*') | |
| call setline(".", l:list_index + 1. ". ") | |
| elseif l:preceding_line =~ '\v^\d+\.\s$' | |
| " The previous line matches any number of digits followed by a full-stop | |
| " followed by one character of whitespace followed by nothing | |
| " i.e. it is an empty ordered list item | |
| " End the list and clear the empty item | |
| call setline(line(".") - 1, "") | |
| elseif l:preceding_line[0] == "-" && l:preceding_line[1] == " " | |
| " The previous line is an unordered list item | |
| if strlen(l:preceding_line) == 2 | |
| " ...which is empty: end the list and clear the empty item | |
| call setline(line(".") - 1, "") | |
| else | |
| " ...which is not empty: continue the list | |
| call setline(".", "- ") | |
| endif | |
| endif | |
| endfunction | |
| " N.B. Currently only enabled for return key in insert mode, not for normal | |
| " mode 'o' or 'O' | |
| inoremap <buffer> <CR> <CR><Esc>:call <SID>auto_list()<CR>A |
I love it, thank you.
Hey, excellent plugin dude!!.. thanks
Hah... I'd totally forgotten this snippet existed! Glad you all like it :). Keep an eye on my still-in-development List Assist plugin if you're interested in a more robust version with more features.
This is exactly what I have been looking for. Thank you so much!
One issue that I am seeing with this is that it doesn't recognize a list item which contains a hard-wrapped block of text. Meaning that if you have a list item that gets hard-wrapped by textwidth, this code snipped will not automatically create a list item when you hit enter.
Another issue that I have just noticed is that it does not recognize nested lists, i.e.
- item 1
- item 2
- subitem 1
- subitem 2
I found this really useful but wanted the same functionality for indented lists so I've added that for myself. I'm not sure what the best way is to share my changes to a gist, but if someone could let me know I'd be happy to make them public.
I'm not sure what the best way is to share my changes to a gist, but if someone could let me know I'd be happy to make them public.
I'm also not sure if there's a better way, but mostly people seem to just publish their own gist and paste a link into a comment on the original.
I've published my changes in a new gist.
Thank you!