yaohunzhanyue / collcetion

6 stars 0 forks source link

ZSaberLv0/ZFVimToc: vim script to quick view TOC (Table Of Contents) for any filetype #16

Open yaohunzhanyue opened 4 years ago

yaohunzhanyue commented 4 years ago

ZFVimToc

vim script to quick view TOC (Table Of Contents) for any filetype

if you like my work, check here for a list of my vim plugins

how to use

  1. use Vundle or any other plugin manager you like to install

    Plugin 'othree/eregex.vim'
    Plugin 'ZSaberLv0/ZFVimToc'
    
  2. use <leader>vt or :ZFToc to view a TOC

    • for configured filetype (see g:ZFVimToc_setting), TOC should show directly
    • for other filetype, you would be asked to enter regexp to search for titles

keymaps

recommended keymap:

autocmd FileType YourFileType nnoremap <silent> <buffer> <leader>vt :call ZF_Toc()<cr>
autocmd FileType YourFileType nnoremap <silent> <buffer> [[ :call ZF_TocPrev('n')<cr>
autocmd FileType YourFileType xnoremap <silent> <buffer> [[ :call ZF_TocPrev('v')<cr>
autocmd FileType YourFileType nnoremap <silent> <buffer> ]] :call ZF_TocNext('n')<cr>
autocmd FileType YourFileType xnoremap <silent> <buffer> ]] :call ZF_TocNext('v')<cr>

by default, the above keymaps would be applied to markdown files, you may disable or change by:

if !exists('g:ZFVimToc_autoKeymap')
    let g:ZFVimToc_autoKeymap={}
endif
let g:ZFVimToc_autoKeymap['markdown']=1

config your own filetype

by default, only markdown file are configured for TOC view, you may add config for your own filetype

let g:ZFVimToc_setting['markdown'] = {
            \     'titleRegExp' : '^[ \t]*[#]+.*$',
            \     'titleLevelRegExpMatch' : '^[ \t]*([#]+).*$',
            \     'titleLevelRegExpReplace' : '\1',
            \     'titleNameRegExpMatch' : '^[ \t]*[#]+[ \t]*(<.*?>)?[ \t]*(.*?)[ \t]*(<.*?>)?[ \t]*$',
            \     'titleNameRegExpReplace' : '\2',
            \     'codeBlockBegin' : '^[ \t]*```.*$',
            \     'codeBlockEnd' : '^[ \t]*```[ \t]*$',
            \ }

patterns:

about pattern regexp:

additional settings

the TOC preview is a location list in fact (:h :lopen)

you may have these settings in your vimrc to make it more convenient:

augroup ZFVimToc_setting
    autocmd!
    autocmd BufWinEnter quickfix
                \  nnoremap <buffer> <silent> q :bd<cr>
                \| nnoremap <buffer> <silent> <leader>vt :bd<cr>
                \| nnoremap <buffer> <silent> <cr> <cr>:lclose<cr>
                \| nnoremap <buffer> <silent> o <cr>:lclose<cr>
                \| setlocal foldmethod=indent
augroup END
yaohunzhanyue commented 4 years ago

W(NJ)CM97BYLK}TQFDF$PXO WLXXSZFTCL9YR8FG }BEIPA