1

I'm trying to get vim to do code folding with my python code and I could really use your help with the last bit here.

I have this in my ~/.vimrc:

set number
"set tabstop=2
"set expandtab
"set sw=2
set nowrap
syntax on
set linebreak
set textwidth=80
set background=dark

" Enable folding
set foldmethod=indent
set foldlevel=99
filetype plugin indent on

" Enable folding with the spacebar
nnoremap <space> za

I've been testing with the following file

from filelock import FileLock
import yaml

def main() -> int:
    """
    main control loop
    """
    #Read config file
    read_config()
    #Lock

I've tried this file with double spaces, four spaces, single tabs, so far nothing has worked. Everything results in "E490 fold not found".

I've tried running set foldmethod=indent after loading the file, I've confirmed that :set syntax returns syntax=python, I've tried a ruby file and code folding works there so it appears to be something about the python I've crafted that it doesn't like. I've tried without the python type hinting (-> int), no change.

I tried installing vim from brew, confirmed I'm using vim from the new path, no change.

I've tried replicating this on a Debian Buster vm so I don't think it's mac specific.

I even tried putting an 'end' which isn't syntactically correct at the end of the function definition just as an experiment, that didn't work either.

This is the output of vim --version

macOS version - x86_64
Included patches: 1-2900
Compiled by Homebrew
Huge version without GUI.  Features included (+) or not (-):
+acl               -farsi             +mouse_sgr         +tag_binary
+arabic            +file_in_path      -mouse_sysmouse    -tag_old_static
+autocmd           +find_in_path      +mouse_urxvt       -tag_any_white
+autochdir         +float             +mouse_xterm       -tcl
-autoservername    +folding           +multi_byte        +termguicolors
-balloon_eval      -footer            +multi_lang        +terminal
+balloon_eval_term +fork()            -mzscheme          +terminfo
-browse            +gettext           +netbeans_intg     +termresponse
++builtin_terms    -hangul_input      +num64             +textobjects
+byte_offset       +iconv             +packages          +textprop
+channel           +insert_expand     +path_extra        +timers
+cindent           +ipv6              +perl              +title
-clientserver      +job               +persistent_undo   -toolbar
+clipboard         +jumplist          +popupwin          +user_commands
+cmdline_compl     +keymap            +postscript        +vartabs
+cmdline_hist      +lambda            +printer           +vertsplit
+cmdline_info      +langmap           +profile           +virtualedit
+comments          +libcall           -python            +visual
+conceal           +linebreak         +python3           +visualextra
+cryptv            +lispindent        +quickfix          +viminfo
+cscope            +listcmds          +reltime           +vreplace
+cursorbind        +localmap          +rightleft         +wildignore
+cursorshape       +lua               +ruby              +wildmenu
+dialog_con        +menu              +scrollbind        +windows
+diff              +mksession         +signs             +writebackup
+digraphs          +modify_fname      +smartindent       -X11
-dnd               +mouse             -sound             -xfontset
-ebcdic            -mouseshape        +spell             -xim
+emacs_tags        +mouse_dec         +startuptime       -xpm
+eval              -mouse_gpm         +statusline        -xsmp
+ex_extra          -mouse_jsbterm     -sun_workshop      -xterm_clipboard
+extra_search      +mouse_netterm     +syntax            -xterm_save
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
  fall-back for $VIM: "/usr/local/share/vim"
Compilation: clang -c -I. -Iproto -DHAVE_CONFIG_H -DMACOS_X -DMACOS_X_DARWIN -g -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: clang -L. -fstack-protector-strong -L/usr/local/lib -L/usr/local/opt/libyaml/lib -L/usr/local/opt/[email protected]/lib -L/usr/local/opt/readline/lib -L/usr/local/lib -o vim -lm -lncurses -liconv -lintl -framework AppKit -L/usr/local/opt/lua/lib -llua5.4 -mmacosx-version-min=11.3 -fstack-protector-strong -L/usr/local/lib -L/usr/local/Cellar/perl/5.34.0/lib/perl5/5.34.0/darwin-thread-multi-2level/CORE -lperl -L/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/lib/python3.9/config-3.9-darwin -lpython3.9 -framework CoreFoundation -lruby.3.0 -L/usr/local/Cellar/ruby/3.0.1/lib

Any ideas how else to poke at or fix this? It's probably something small and silly so Ill apologize now, I just can't see it at the moment.. Thanks in advance!

References:

Update1: Added filetype plugin indent on to vimrc, recommended in comments.

1

2 Answers 2

1

EDIT You also need to add the following line to your vimrc:

filetype plugin indent on

See :h :filetype-plugin-on and :h :filetype-indent-on this command enable the filetype detection and the loading of the indent scripts which are necessary to have working folding.


You can't set foldmethod directly from the vimrc, see :h 'fdm':

'foldmethod' 'fdm'  string (default: "manual")
            local to window

The key words here are local to window: Setting the fold method in your vimrc will not affect the other splits you open.

You need to create a ftplugin :h ftplugin for python and put the setting in it. Create ~/.vim/after/ftplugin/python.vim and put your set foldmethod=indent there.

8
  • Btw I'm 100% sure that we have at least one duplicate of this question but I can't find it
    – statox
    Jun 1, 2021 at 8:34
  • That didn't work for me, putting 'set foldmethod=indent' as the contents of ~/.vim/after/ftplugin/python.vim. I mentioned in my post but I also tried 'set foldmethod=indent' in vim after loading the file, I don't think this is a vimrc problem but I could be mistaken. I appreciate your help! Jun 1, 2021 at 8:37
  • 1
    Ah right @JeffWelling you forgot to put filetype plugin indent on in your vimrc that's why it doesn't work I think
    – statox
    Jun 1, 2021 at 8:45
  • Hm, I added filetype plugin indent on right below set foldlevel=99 but that doesn't seem to have done it either, I appreciate your help though @statox! I also tried doing 'set foldmethod=indent' after loading the file, to your point about it being local to the window, no change. This was with ftplugin/python.vim in place and rm'd, no change. Jun 1, 2021 at 15:12
  • Now that's weird... I don't have an idea of what else could be wrong. Since it doesn't work only for python files maybe there is something with the built-in python ftplugin but since you use indent I don't see how that would play.
    – statox
    Jun 1, 2021 at 15:44
0

With the settings you have it should work.

Press zM to close all folds. Note if you press za on def main it would not toggle the fold -- it doesn't exist on that line. Put your cursor on indented line and press za there

enter image description here

enter image description here PS

Make sure you have matching shiftwidth to your actual indent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.