What is the recommended way to use Vim folding for Python code

后端 未结 11 2089
自闭症患者
自闭症患者 2020-12-22 16:33

I am interested in enabling code folding in Vim for Python code. I have noticed multiple ways to do so.

Does anyone have a preferred way to do Python code folding in

11条回答
  •  醉话见心
    2020-12-22 17:06

    For me the ideal folding is to fold just the class and def blocks, indent folding is too much for my taste. I think one elegant solution is to use the syntax system like this one mentioned by Tomas. However, this one is meant to replace the original syntax file and it may end being older than the original (i.e. that script doesn't mention Python 3 syntax).

    My solution is to place in the ~/.vim/syntax folder a file named python.vim with just the important lines (taken from the above script):

    syn match   pythonDefStatement  /^\s*\%(def\|class\)/
           \ nextgroup=pythonFunction skipwhite
    syn region  pythonFunctionFold  start="^\z(\s*\)\%(def\|class\)\>"
           \ end="\ze\%(\s*\n\)\+\%(\z1\s\)\@!." fold transparent
    
    hi link pythonDefStatement Statement
    

    Then simply activate the folding with :set foldmethod=syntax.

提交回复
热议问题