标签归档:folding

使用Vim折叠进行Python代码的推荐方法是什么

问题:使用Vim折叠进行Python代码的推荐方法是什么

我对在Vim中为Python代码启用代码折叠感兴趣。我注意到有多种方法可以做到这一点。

有没有人喜欢在Vim中进行Python代码折叠的首选方法?即

  • 您是否有喜欢并喜欢的特定Vim插件?
  • 您使用手动折叠还是在标记中放置标记?
  • 还有其他推荐的方式在Vim中为Python进行代码折叠吗?

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 Vim? I.e,

  • Do you have a particular Vim plugin that you use and like?
  • Do you use manual folding or do you place markers in comments?
  • Any other recommended ways to do code folding for Python in Vim?

回答 0

就我个人而言,我无法说服自己用标记乱丢我的代码。我已经习惯于(高效)使用缩进折叠。连同我的空格键映射(请参见下文)以打开/关闭折叠以及zR和zM命令,我就在家里。非常适合Python!

set foldmethod=indent
nnoremap <space> za
vnoremap <space> zf

Personally I can’t convince myself to litter my code with the markers. I’ve become pretty used to (and efficient) at using indent-folding. Together with my mapping of space bar (see below) to open/close folds and the zR and zM commands, I’m right at home. Perfect for Python!

set foldmethod=indent
nnoremap <space> za
vnoremap <space> zf

回答 1

将此语法文件用于Python。它将折叠方法设置为语法,并折叠所有类和函数,但仅此而已。

I use this syntax file for Python. It sets the folding method to syntax and folds all classes and functions, but nothing else.


回答 2

另一个用于折叠Python代码的插件。在GitHub上,处理文档字符串相当简单。

SimpylFold

请享用!

Yet another plugin for folding Python code. Rather simple, handling docstrings, and on the GitHub:

SimpylFold

Enjoy!


回答 3

Python非常适合折叠缩进,有点适合编写我自己的代码,我使用标记,因为它们可以按照您想要的方式压缩文档,并且可以用作目录。我在查看其他人的代码时,在vimrc中可以在两者之间切换。

#Toggle fold methods \fo
let g:FoldMethod = 0
map <leader>fo :call ToggleFold()<cr>
fun! ToggleFold()
    if g:FoldMethod == 0
        exe 'set foldmethod=indent'
        let g:FoldMethod = 1
    else
        exe 'set foldmethod=marker'
        let g:FoldMethod = 0
    endif
endfun
#Add markers (trigger on class Foo line)
nnoremap ,f2 ^wywO#<c-r>0 {{{2<esc>
nnoremap ,f3 ^wywO#<c-r>0 {{{3<esc> 
nnoremap ,f4 ^wywO#<c-r>0 {{{4<esc>
nnoremap ,f1 ^wywO#<c-r>0 {{{1<esc>

Python is well suited for folding on indent, bit for writing my own code I use markers as they can crunch a document down the way you want it and can serve as a kind of a table of contents. I have this in my vimrc to flip between the two when I’m viewing someone elses code.

#Toggle fold methods \fo
let g:FoldMethod = 0
map <leader>fo :call ToggleFold()<cr>
fun! ToggleFold()
    if g:FoldMethod == 0
        exe 'set foldmethod=indent'
        let g:FoldMethod = 1
    else
        exe 'set foldmethod=marker'
        let g:FoldMethod = 0
    endif
endfun
#Add markers (trigger on class Foo line)
nnoremap ,f2 ^wywO#<c-r>0 {{{2<esc>
nnoremap ,f3 ^wywO#<c-r>0 {{{3<esc> 
nnoremap ,f4 ^wywO#<c-r>0 {{{4<esc>
nnoremap ,f1 ^wywO#<c-r>0 {{{1<esc>

回答 4

我认为缩进折叠适合python。我正在为vim-config python / django IDE思想制作一个多分支git repo。叉开!

http://github.com/skyl/vim-config-python-ide

I think that indent folding is fine for python. I’m making a multi-branched git repo for vim-config python/django IDE ideas. Fork away!

http://github.com/skyl/vim-config-python-ide


回答 5

我真的很喜欢这个python_ifold插件


回答 6


回答 7

对我来说,理想的折叠方式是只折叠 classdef折叠,缩进式折叠对于我的口味而言实在太多了。我认为,一个优雅的解决方案是使用的语法系统,这样一个由Tomas提及。但是,该脚本旨在替换原始语法文件,并且最终可能会比原始语法文件旧(即该脚本未提及Python 3语法)。

我的解决方案是将 ~/.vim/syntax文件夹中一个python.vim仅包含重要行的文件(取自上面的脚本):

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

然后只需使用激活折叠即可:set foldmethod=syntax

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.


回答 8

Python源代码附带一个vim语法插件以及一个自定义vimrc文件。检查vim上python常见问题

The Python source comes with a vim syntax plugin along with a custom vimrc file. Check the python FAQ on vim


回答 9

在您的.vimrc

set foldmethod=indent
set shiftwidth=4

然后zM掩盖所有内容zR以展开所有内容。我还补充说:

nnoremap <space> za
vnoremap <space> zf
map z1  :set foldlevel=0<CR><Esc>
map z2  :set foldlevel=1<CR><Esc>
map z3  :set foldlevel=2<CR><Esc>
map z4  :set foldlevel=3<CR><Esc>
map z5  :set foldlevel=4<CR><Esc>
map z6  :set foldlevel=5<CR><Esc>
map z7  :set foldlevel=6<CR><Esc>
map z8  :set foldlevel=7<CR><Esc>
map z9  :set foldlevel=8<CR><Esc>

这样你就可以z1z2被取消缩进少一点。

In your .vimrc:

set foldmethod=indent
set shiftwidth=4

Then zM to mask all zR to expand all. I also added:

nnoremap <space> za
vnoremap <space> zf
map z1  :set foldlevel=0<CR><Esc>
map z2  :set foldlevel=1<CR><Esc>
map z3  :set foldlevel=2<CR><Esc>
map z4  :set foldlevel=3<CR><Esc>
map z5  :set foldlevel=4<CR><Esc>
map z6  :set foldlevel=5<CR><Esc>
map z7  :set foldlevel=6<CR><Esc>
map z8  :set foldlevel=7<CR><Esc>
map z9  :set foldlevel=8<CR><Esc>

So you can z1 and z2 to unindent little by little.


回答 10

我真的很喜欢我为.vimrc编写的这个小vim脚本。它映射alt+1为折叠第一个python缩进级别(类定义和函数),alt+2折叠第二个python缩进级别(类方法)以及alt+0展开所有内容。它确保仅折叠一个级别,而不折叠任何嵌套的子级别。您仍然可以使用za切换当前块的折叠。请注意,中^[0^[alt用于我的终端。对vim脚本没有太多的经验,所以可以对函数提出建议:)

" Python folding
nnoremap ^[0 zR<CR>
nnoremap ^[1 :call Fold(0)<CR>
nnoremap ^[2 :call Fold(1)<CR>
function Fold(level)
    :let b:max = a:level + 1
    :set foldmethod=indent
    :execute 'set foldnestmax='.b:max
    :execute 'set foldlevel='.a:level
endfunction

I really like this little vim script i wrote for .vimrc. It maps alt+1 to fold the first python indent level (class definitions and functions), alt+2 to fold the second level (class methods), and alt+0 to unfold everything. It makes sure it only folds one level and doesn’t fold any of the nested sub levels. You can still use za to toggle folding for the current block. Note that in ^[0, the ^[ is alt for my terminal. Don’t have a lot of experience in vim script so feel free to make suggestions on the function :)

" Python folding
nnoremap ^[0 zR<CR>
nnoremap ^[1 :call Fold(0)<CR>
nnoremap ^[2 :call Fold(1)<CR>
function Fold(level)
    :let b:max = a:level + 1
    :set foldmethod=indent
    :execute 'set foldnestmax='.b:max
    :execute 'set foldlevel='.a:level
endfunction