Vim Tips Wiki
Register
Advertisement

Previous TipNext Tip

Tip: #741 - Syntax folding of Python files

Created: June 7, 2004 8:49 Complexity: intermediate Author: Lee Chun Kin Version: 6.0 Karma: 21/11 Imported from: Tip#741

With the folding function of Vim6 (+folding), we can edit the Python programmes similar to outline program editors such as SciTE.} Add follow lines to .vimrc:

augroup python_prog 
  au! 
  fun! Python_fold() 
    execute 'syntax clear pythonStatement' 
    execute 'syntax keyword pythonStatement break continue del' 
    execute 'syntax keyword pythonStatement except exec finally' 
    execute 'syntax keyword pythonStatement pass print raise' 
    execute 'syntax keyword pythonStatement return try' 
    execute 'syntax keyword pythonStatement global assert' 
    execute 'syntax keyword pythonStatement lambda yield' 
    execute 'syntax match pythonStatement /\<def\>/ nextgroup=pythonFunction skipwhite' 
    execute 'syntax match pythonStatement /\<class\>/ nextgroup=pythonFunction skipwhite' 
    execute 'syntax region pythonFold start="^\z(\s*\)\%(class\|def\)" end="^\%(\n*\z1\s\)\@!" transparent fold' 
    execute 'syntax sync minlines=2000 maxlines=4000' 
    set autoindent 
    set foldmethod=syntax 
    " set foldopen=all foldclose=all 
    set foldtext=substitute(getline(v:foldstart),'\\t','\ \ \ \ ','g') 
    set fillchars=vert:\|,fold:\ 
    set tabstop=4 shiftwidth=4 nowrap guioptions+=b 
  endfun 
  
  autocmd FileType python call Python_fold() 
augroup END 

You can use the folding functions (zm, zM, zr, zR, xa, zo, zc, zx...) in editing Python program.

Comments

Or you could use the python_fold script (script#515)


Advertisement