On an up-to-date Arch system, I set up marker-based folds in vim
and python
scripts like so:
Extracted from my ~/.vimrc
[1]
set nocompatible " default
filetype plugin indent on
set nofoldenable
set foldlevel=1
set foldcolumn=1
Along with:
$ cat ~/.vim/after/ftplugin/vim.vim
setlocal foldmethod=marker
setlocal foldmarker={{{,}}} " Default for fdm=marker
$ cat ~/.vim/after/ftplugin/python.vim
setlocal foldmethod=marker
setlocal foldmarker={{{,}}} " Default for fdm=marker
setlocal foldnestmax=3
It works for vim scripts but not for python. I've been at it most of one day already and frankly I am missing something. I am currently resorting to committing the content of ~/.vim/after/ftplugin/python.vim
directly to ~/.vimrc
and setting the corresponding parameters globally instead of locally. That works well, but that also excludes the possibility of differentiating settings, e.g.:
- marker-based folds in
vim
- indent-based or syntax-based folds in
python
or inbash
scripts.
What am I doing wrong ?
With all setting committed globaly to ~/.vimrc
, what I get in the vim
buffer of any python module is:
: verbose set filetype? foldmethod? foldmarker?
filetype=python
Last set from /usr/share/vim/vim82/filetype.vim line 1291
foldmethod=marker
Last set from ~/.vim/view/~=+Documents=+Work=+Projects=+xxx-data=+Src=+proof_demo.py= line 67
foldmarker={{{,}}}
Last set from ~/.vim/view/~=+Documents=+Work=+Projects=+xxx-data=+Src=+proof_demo.py= line 66
What "it doesn't work" means
It doesn't fold at all when I do zc
, other than where I have previously set fold(s) manually. zM
has no effect at all (for example when foldmethod=indent
in ~/.vim/after/ftplugin/python.vim
), again except when markers are set manually (provided foldmethod=marker
in ~/.vimrc
). Same behavior for all python files, which tells me it is probably some deep-seated issue, not malformed python file. Same in shell scripts with a similarly built ~/.vim/after/ftplugin/sh.vim
, so it's not python specific.
Views info
The most interesting use case in that context is when foldmethod=manual
because, without views, manually set folds are lost upon closing the buffer. But I need views to be able to go back to the buffer's last cursor position (line + col) when re-opening a file with vim. I need views for that because the alternative I did implement before I started using folds, based on vim.fandom.com/wiki/…, is incompatible with implementing any kind of folds apparently. It just fails silently.
It turns out my use of "views" is not compatible with parsing settings from ~/.vim/after/ftplugin/*.vim
files. "Why ?" is beyond me. I am not sure whether it is the execution context I build by means of my ~/.vimrc
or a bug. I would tend to favor the first, although I really don't have much special in ~/.vimrc
.
:verbose set filetype? foldmethod? foldmarker?
return? Wondering if any of your settings are being overwritten elsewhere... When you say it doesn't work, what do you mean? It doesn't fold at all? It folds the incorrect blocks? What if you use a command such aszM
to fold everything? Can you share a small reproducer Python file with the markers where this doesn't do what you expect it to?foldmethod=marker
, you have to add the markers (manually) for folding to work... Like, you need to add a# {{{
comment to begin a fold and a# }}}
to end it. (Or a# {{{1
comment at each new top-level fold, using an explicit level also works.) Do you by any chance misunderstand howfoldmethod=marker
is supposed to work? See:help fold-marker
...