VIM List Mode

By Vitaly Parnas

I spend much time within “list” type of files, that is, files with indented lists, further sub-lists, and so on.

VIM features a fold method indent to automatically create folds based on indentations, making browsing lists similar to browsing a directory tree in a file manager; the list items (directory contents) remain closed until viewed. In effect, you can explore the forest without being yet distracted by the trees.

I created a ‘ListMode’ script enabling the toggling of this feature as well as a set of convenient remappings. With the mode enabled:

  1. ENTER and SPACE toggle the opening and closing of a list, saving one key stroke from the standard ‘za’.
  2. ‘{’ and ‘}’ navigate between lists (and sub-lists), rather than paragraphs.
  3. ‘[’ and ‘]’ transitions between the beginning and end of the current list.

Place the following in ~/.vim/scripts/listbrowse.vim.

autocmd BufAdd,BufRead * let b:ListMode = 0

function! ToggleListMode()
    if !b:ListMode
        call ListModeOn()
        call ListModeOff()

function! ListModeOn()
    echo "ListMode: On"
    let b:ListMode=1
    set foldmethod=indent
    setlocal foldignore=

    noremap <buffer> } zj
    noremap <buffer> { zk
    noremap <buffer> ] ]z
    noremap <buffer> [ [z
    noremap <buffer> <return> za
    noremap <buffer> <space> za

function! ListModeOff()
    echo "ListMode: Off"
    let b:ListMode=0
    set foldmethod=manual
    setlocal foldignore=# " default value

    noremap <buffer> } }
    noremap <buffer> { {
    noremap <buffer> ] ]
    noremap <buffer> [ [
    noremap <buffer> <return> <return>
    noremap <buffer> <space> <space>

Source the script (and any others) in .vimrc:

:runtime! scripts/*.vim

Map F4, or your preferred key, to toggle ListMode:

nmap <F4> :call ToggleListMode()<CR>

Questions, comments? Connect.