Skip to content

Instantly share code, notes, and snippets.

@sedm0784
Last active March 28, 2025 20:04
Show Gist options
  • Select an option

  • Save sedm0784/dffda43bcfb4728f8e90 to your computer and use it in GitHub Desktop.

Select an option

Save sedm0784/dffda43bcfb4728f8e90 to your computer and use it in GitHub Desktop.
Vim Automatic List Continuation
" 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
@nhooyr
Copy link

nhooyr commented Mar 5, 2017

Thank you!

@DamienOConnell
Copy link

I love it, thank you.

@Ramkumar47
Copy link

Hey, excellent plugin dude!!.. thanks

@sedm0784
Copy link
Author

sedm0784 commented Sep 3, 2020

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.

@K4LCIFER
Copy link

This is exactly what I have been looking for. Thank you so much!

@K4LCIFER
Copy link

K4LCIFER commented Jul 29, 2021

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

@kipawaa
Copy link

kipawaa commented Jan 15, 2025

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.

@sedm0784
Copy link
Author

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.

@kipawaa
Copy link

kipawaa commented Mar 28, 2025

I've published my changes in a new gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment