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

发布于 2024-07-11 06:31:22 字数 1710 浏览 7 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

萌无敌 2024-07-18 06:31:23

就我个人而言,我无法说服自己在代码中乱扔标记。 我已经非常习惯(并且高效)使用缩进折叠。 加上我用空格键(见下文)打开/关闭折叠以及 zR 和 zM 命令的映射,我就如在家中一样。 非常适合Python!

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

这映射空格键以在正常模式下打开/关闭当前缩进折叠。 它还映射空格键以在视觉模式下创建手动折叠。 但是,如果将 Foldmethod 设置为缩进,则此方法不起作用,因此其目的是一个谜。

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

This maps the spacebar to open/close the current indented fold in normal mode. It also maps the spacebar to create a manual fold in visual mode. However, this doesn't work if foldmethod is set to indent so its purpose is a mystery.

埖埖迣鎅 2024-07-18 06:31:23

我使用 Python 的 this 语法文件。 它将折叠方法设置为语法并折叠所有类和函数,但不折叠其他内容。

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

兲鉂ぱ嘚淚 2024-07-18 06:31:23

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

SimpylFold

享受吧!

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

SimpylFold

Enjoy!

回首观望 2024-07-18 06:31:23

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>
感受沵的脚步 2024-07-18 06:31:23

我真的很喜欢 python_ifold 插件

I really like the python_ifold plugin.

猫腻 2024-07-18 06:31:23

在您的 .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.

表情可笑 2024-07-18 06:31:23

对我来说,理想的折叠是只折叠 classdef 块,缩进折叠对我来说太不合口味了。 我认为一个优雅的解决方案是使用像这样的语法系统 one 提到由托马斯. 然而,这个文件旨在替换原始语法文件,并且它可能会比原始文件更旧(即该脚本没有提到 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.

木緿 2024-07-18 06:31:23

我真的很喜欢我为 .vimrc 编写的这个 vim 小脚本。 它映射 alt+1 来折叠第一个 python 缩进级别(类定义和函数),alt+2 来折叠第二级(类方法),以及 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文