Giter Club home page Giter Club logo

zfvimfoldblock's Introduction

ZFVimFoldBlock

vim script to fold code blocks using regexp

if you like my work, check here for a list of my vim plugins, or buy me a coffee

preview

  • see all virtual functions :ZFFoldBlock //virtual/

    example0

  • filter out all comments :ZFFoldBlock /^[ \t]*#//

    example1

  • fold by xml tags :ZFFoldBlock /<([a-z0-9_:\.]+)/<\/\l1>|\/>/ (with othree/eregex.vim)

    example2

how to use

  1. use Vundle or any other plugin manager is recommended

    Plugin 'ZSaberLv0/ZFVimFoldBlock'
    

    when othree/eregex.vim installed (recommended), perl style regexp would be used instead of vim style

    Plugin 'othree/eregex.vim'
    
  2. foldmethod must be set to manual

    set foldmethod = manual
    

    by default, foldmethod would be set to manual when ZFFoldBlock called, you may disable it by let g:ZFVimFoldBlock_resetFoldmethodWhenUse=0

  3. foldminlines recommended set to 0, which allow fold even for single line

    set foldminlines=0
    

    by default, foldminlines would be set to 0 when ZFFoldBlock called, you may disable it by let g:ZFVimFoldBlock_resetFoldminlinesWhenUse=0

  4. recommended to have these key map:

    nnoremap ZB q::call ZF_FoldBlockTemplate()<cr>
    nnoremap ZF :ZFFoldBlock //<left>
    
  5. or, use the functions directly:

    " fold by begin and end regexp
    :ZFFoldBlock /begin_regexp/end_regexp/
    
    " same as :ZFFoldBlock /regexp//
    :ZFFoldIfMatch regexp
    
    " same as :ZFFoldBlock //regexp/
    :ZFFoldIfNotMatch regexp
    

sample

you may :call ZF_FoldBlockHelp() to show sample at any time

  • /{/}/ : normal block mode

    {
        in fold 1
        {
            in fold 2
        }
        in fold 1
    }
    
  • /rem// : fold if match

    rem in fold
    rem in fold
    
  • //rem/ : fold if not match

    in fold
    in fold
    rem not in fold
    rem not in fold
    
  • /tag/tag/ : single tag mode

    tag
        in fold 1
    tag
    not in fold
    tag
        in fold 2
    tag
    

regexp

use vim's regexp by default, see :h magic

if you have othree/eregex.vim installed, then perl style regexp would be used instead

you may force disable it by let g:ZFVimFoldBlock_disableE2v = 1

advanced

  • typical advanced usage:

    " fold comments accorrding to file type
    function! ZF_Plugin_ZFVimFoldBlock_comment()
        let expr='\(^\s*\/\/\)'
        if &filetype=='vim'
            let expr.='\|\(^\s*"\)'
        endif
        if &filetype=='c' || &filetype=='cpp'
            let expr.='\|\(^\s*\(\(\/\*\)\|\(\*\)\)\)'
        endif
        if &filetype=='make'
            let expr.='\|\(^\s*#\)'
        endif
        let disableE2vSaved = g:ZFVimFoldBlock_disableE2v
        let g:ZFVimFoldBlock_disableE2v = 1
        call ZF_FoldBlock("/" . expr . "//")
        let g:ZFVimFoldBlock_disableE2v = disableE2vSaved
        echo "comments folded"
    endfunction
    nnoremap ZC :call ZF_Plugin_ZFVimFoldBlock_comment()<cr>
    
  • calling ZFFoldBlock would append fold instead of replace, before using the function, you may want to remove all fold by zE or zD manually

  • when fold with /expr_l/expr_r/ format, there's a special pattern for expr_r to reference submatches in expr_l (similar to :h /\1), the pattern format is \lN, where N is 1~9

    for example, /<([a-z]+)>/</\l1>/ would result:

    xxx   // not in fold
    <aa>  // in fold
      xxx // in fold
    </aa> // in fold
    xxx   // not in fold
    

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.