Created
March 28, 2025 20:02
-
-
Save kipawaa/a2e5a6a0c33329ec3b4e949a8edf9d69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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() | |
" get previous line | |
let l:prev_line = getline(line(".") - 1) | |
" get the leading whitespace from the previous line | |
let l:whitespace = matchstr(l:prev_line, '\v^\s*') | |
" if previous line is a non-empty ordered list item | |
if l:prev_line =~ '\v^\s*\d+\.\s\S+.*$' | |
" get the list index | |
let l:whitespace_and_list_index = matchstr(l:prev_line, '\v^\s*\d*') | |
let l:list_index = matchstr(l:whitespace_and_list_index, '\v\d+') | |
" this works since the list index must be at the start of the line | |
" Continue the list | |
call setline(".", l:whitespace . (l:list_index + 1) . ". ") | |
" if previous line is an empty ordered list item | |
elseif l:prev_line =~ '\v^\s*\d+\.\s*$' | |
" End the list and clear the empty item | |
call setline(line(".") - 1, "") | |
" if the previous line is a non-empty unordered list item | |
elseif l:prev_line =~ '\v^\s*\-\s.+$' | |
" continue the list | |
call setline(".", l:whitespace . "- ") | |
" if the previous line is an empty unordered list item | |
elseif l:prev_line =~ '\v^\s*\-\s*$' | |
" end the list and clear the empty item | |
call setline(line(".") - 1, "") | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an update to sedm0784's auto-list that works for indented lists. I've tried to comment and write this in such a way that it can be easily for other list types/styles etc.