Skip to content

Instantly share code, notes, and snippets.

@sedm0784
Last active March 28, 2025 20:04
Show Gist options
  • Save sedm0784/dffda43bcfb4728f8e90 to your computer and use it in GitHub Desktop.
Save sedm0784/dffda43bcfb4728f8e90 to your computer and use it in GitHub Desktop.

Revisions

  1. sedm0784 revised this gist Sep 3, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _vim_auto_list.markdown
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ If you're not sure how to install, just drop the `markdown.vim` file itself in `

    # N.B.

    1. I just use this for Markdown, but the code itself doesn't care about the `filetype`. It should be fairly easy to adjust it for different list styles. Making it a robust, multi-format extension would be a bit more work.
    1. I just use this for Markdown, but the code itself doesn't care about the `filetype`. It should be fairly easy to adjust it for different list styles. Making it a [robust, multi-format extension would be a bit more work](https://github.com/sedm0784/vim-list-assist).
    2. It supports lists with markers like `-` and `1.`, because those are the list markers I use, but it would be easy to make it support more types of bullet/numbering.
    3. I've just got it enabled for when I hit <kbd>Return</kbd>, but it'd be fairly easy to also enable it for normal mode <kbd>o</kbd> and <kbd>O</kbd>.

  2. sedm0784 revised this gist Oct 27, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion _vim_auto_list.markdown
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ It supports ordered lists with markers like `1.` and unordered lists with `-` ma

    # Installation

    You can either drop the file itself in `~/vim/after/markdown.vim`, or if that file already exists, just copy the code into it.
    If you're not sure how to install, just drop the `markdown.vim` file itself in `~/vim/after/`, or if that file already exists, copy the code into it.

    # N.B.

  3. sedm0784 renamed this gist Oct 27, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. sedm0784 renamed this gist Oct 27, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. sedm0784 renamed this gist Oct 27, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. sedm0784 revised this gist Oct 27, 2015. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions README.markdown
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    # Vim Auto List Completion

    This snippet makes Vim automatically continue/end lists in insert mode, similar to the way word processors do:

    - It automatically adds the bullet/number for the next list item when you press <kbd>Return</kbd> at the end of an existing item,
    - When you press <kbd>Return</kbd> on an empty list item, it removes the bullet/number, ending the list.

    It supports ordered lists with markers like `1.` and unordered lists with `-` markers (because those are the markers I use.)

    (It's particularly useful when using an iOS keyboard where punctuation and numerals are slow to access.)

    # Installation

    You can either drop the file itself in `~/vim/after/markdown.vim`, or if that file already exists, just copy the code into it.

    # N.B.

    1. I just use this for Markdown, but the code itself doesn't care about the `filetype`. It should be fairly easy to adjust it for different list styles. Making it a robust, multi-format extension would be a bit more work.
    2. It supports lists with markers like `-` and `1.`, because those are the list markers I use, but it would be easy to make it support more types of bullet/numbering.
    3. I've just got it enabled for when I hit <kbd>Return</kbd>, but it'd be fairly easy to also enable it for normal mode <kbd>o</kbd> and <kbd>O</kbd>.

    Let me know if you want to tweak it but don't know how to.
  7. sedm0784 created this gist Oct 27, 2015.
    34 changes: 34 additions & 0 deletions markdown.vim
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    " 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