I think, therefore I blog

VimFlowy: The Simplest Outliner for VIM

By , 6 March 2016

VimFlowy: The Simplest Outliner for VIM
VimFlowy: The Simplest Outliner for VIM

I was quite fond of the WorkFlowy outliner until the sync failed and broke my offline access. It was just lucky that I'd made an export of my data not too long before it failed.

But really, I didn't need sync that much. I just needed a fast outliner. And what could faster than Vim? With a bit of investigation into Vim's code folding capabilities, I came up with a single-file script to turn Vim into a fast and simple outliner.

To install VimFlowy, just copy vimflowy.vim to your $HOME/.vim/ftdetect directory and use a .out extension for your outliner files. You can also use :set filetype=vimflowy if you prefer not to use the .out extension.

Download vimflowy.vim to $HOME/.vim/ftdetect

Here is what it looks like. You just use <TAB> to open and close the folds, and navigate through your outline.

Here is the full code of vimflowy.vim for the curious.

Enjoy!

"
" VimFlowy is the simplest outliner for vim. It uses vim's existing code 
" folding capabilities with some simple configuration. The result is a fast,
" powerful outliner using your favourite text editor.
"
" Install VimFlowy by saving this file to $HOME/.vim/ftdetect/vimflowy.vim
" on unix, or $HOME/vimfiles/ftdetect/vimflowy.vim on Windows.
"
" Save your outliner files with a .out extension for VimFlowy to be 
" autodetected. Otherwise, use :set filetype=vimflowy from within vim.
"
" The outliner uses an indentation level of 2 white spaces to create 
" new levels. You can use vim's default code folding shortcuts to move
" throughout your outline, or just use <TAB> to open and close levels.
" 
" The most frequent shortcut keys you will use are:
"
" <TAB> open or close the current fold
"  zx  close all other folds
"  dd  to delete a fold (when it is closed)
"  [p  to paste it at the current indent level (use with dd to move outlines)
"
"
" Use :help fold-commands in vim for additional shorcuts.
" 
" The fold function consumes blank lines. If you need to separate one
" fold from another, use a single dot or a dash on a line by itself.
"
" Check for updates to VimFlowy at https://rogerkeays.com/vimflowy
"
autocmd BufRead,BufNewFile *.out set filetype=vimflowy
autocmd FileType vimflowy set foldmethod=expr foldexpr=VimFlowyFold(v:lnum)
autocmd FileType vimflowy set foldtext=getline(v:foldstart)
autocmd FileType vimflowy set shiftwidth=2 expandtab autoindent
autocmd FileType vimflowy hi Folded ctermbg=black ctermfg=yellow
autocmd FileType vimflowy nnoremap <TAB> za

function! VimFlowyFold(lnum)
    if getline(a:lnum) =~? '\v^\s*$'
        return VimFlowyFold(a:lnum - 1)
    endif

    let this_indent = indent(a:lnum) / &shiftwidth
    let next_indent = indent(NextNonBlankLine(a:lnum)) / &shiftwidth

    if next_indent == this_indent
        return this_indent
    elseif next_indent < this_indent
        return this_indent
    elseif next_indent > this_indent
        return '>' . next_indent
    endif
endfunction

function! NextNonBlankLine(lnum)
    let numlines = line('$')
    let current = a:lnum + 1

    while current <= numlines
        if getline(current) =~? '\v\S'
            return current
        endif

        let current += 1
    endwhile

    return -2
endfunction
VimFlowy: The Simplest Outliner for VIM
 

About Roger Keays

VimFlowy: The Simplest Outliner for VIM

Roger Keays is an artist, an engineer, and a student of life. Since he left Australia in 2009, he has been living as a digital nomad in over 40 different countries around the world. Roger is addicted to surfing. His other interests are music, psychology, languages, and finding good food. Click here to subscribe to his weekly blog, or stalk him on Facebook and Twitter.

Leave a Comment

Please visit https://RogerKeays.com/vimflowy to add your comments.

Comment posted by: Jeff Wu, 6 months ago

Are you aware of this?  https://github.com/WuTheFWasThat/vimflowy

Comment posted by: otheus, 9 months ago

Very cool.

Join 3,559 People Who Think Outside The Box

I write every Sunday about travel, psychology, technology, and all sorts of interesting stuff. It's completely free, and you can subscribe for as long as you like. Do it now, so you don't miss a single post.

Chat For A While

Your Vote Matters

Which Tech Company is the Creepiest?
Facebook
Microsoft
Google
Amazon
Wikipedia
Wikileaks
Redtube