137
  1. When I open the file it looks like this: enter image description here or even this enter image description here
  2. When I open all folds, they are closed again when I navigated to another buffer and came back.
  3. To be able to work with it, I have to apply zR each time when opening a buffer.

I have these set up in .vimrc:

set foldlevelstart=99
set foldlevel=99

Please point me on how to disable the folding, or at least making the navigation to another buffer not to close the opened ones.

lyuba
  • 6,250
  • 7
  • 27
  • 37
  • 3
    When first starting vim, run `:set foldlevel` and `:set foldlevelstart` to see what they are set at. My inital guess is that these values are getting reset somewhere. When I add those values to my vimrc, everything works as expected. – cledoux Feb 16 '11 at 14:54

9 Answers9

247

You're not alone.

set nofoldenable    " disable folding
zeuxcg
  • 9,216
  • 1
  • 26
  • 33
  • 13
    Note that this doesn't affect vimdiff. When vim creates a diff window it seems to override the `foldenable` option. The only workaround I've found is to set the `context` sub-option of `diffopt` to something really huge. eg: `set diffopt+=context:99999` – Laurence Gonsalves Mar 26 '14 at 22:51
  • This doesn't work for me. Perhaps something is overriding it. – BenjaminGolder Apr 23 '14 at 15:38
  • 17
    it was [vim-markdown](https://github.com/plasticboy/vim-markdown#options). To disable folding by vim-markdown, use `let g:vim_markdown_folding_disabled=1` – BenjaminGolder Apr 23 '14 at 15:43
  • 1
    @BenjaminGolder we have fixed that at: https://github.com/plasticboy/vim-markdown/pull/131 – Ciro Santilli OurBigBook.com Aug 16 '15 at 22:19
  • 3
    I like glts' solution below better: use 'zi' to toggle foldenable. One of the few BIG problem with vim is the sheer number of things to remember. Little things like 'zi' instead of 'foldenable' are god-sent! – Mugabo Aug 23 '15 at 14:53
  • 1
    doesnt seem to work for me :( python code is still folded. – weima Jun 20 '17 at 13:01
  • 1
    The stock version of vim-awesome folded everything as well, BenjaminGolder's suggestion fixed it `let g:vim_markdown_folding_disabled=1`. – slm Sep 18 '18 at 13:00
  • In my case this also required removing `~/.vim/view` files. – Błażej Michalik Mar 19 '22 at 09:54
65

The easiest way to disable (and enable) folding on the fly is zi.

zi is the normal mode command that toggles 'foldenable', just like :set foldenable!.

Mnemonic: "fold invert". See :h zi.

glts
  • 21,808
  • 12
  • 73
  • 94
46

Add set nofoldenable to your ~/.vimrc to disable folding.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • @Magnus: That's deliberately done by `vimdiff` to avoid showing lots of lines that are identical. The use case is different, so it's not affected by the `nofoldenable`. `zR` opens all folds; I do not know about a setting that disables folding in `vimdiff`. (As it's the one place where folding doesn't bother me.) – DevSolar Mar 17 '14 at 14:43
16

Here is an article which briefly and concisely sums up why folding is cool. The one line reason is that folding makes navigating very large files a breeze.

If you want to leave folding enabled, and simply always start with all folds open, the vim wiki tells how. The method of interest to you would probably be the autocommand method.

" Tweak the event and filetypes matched to your liking. 
" Note, perl automatically sets foldmethod in the syntax file
autocmd Syntax c,cpp,vim,xml,html,xhtml setlocal foldmethod=syntax
autocmd Syntax c,cpp,vim,xml,html,xhtml,perl normal zR

I would also recommend searching for custom folding methods for the language you use. Simply googling "vim <insert language here> folding" should bring up a number of options. Play around with the different options until you find a folding method you like.

cledoux
  • 4,717
  • 1
  • 22
  • 30
5

I have added this line to my .vimrc file cause I had the same issue:

autocmd FileType * exe "normal zR"

This command will be executed every time you open a file automatically. So you won't see the bug and the folding feature won't be lost too)

zinovyev
  • 2,084
  • 1
  • 22
  • 32
3

Just adding one more to make it complete to the point of discussion.

To enable code folding:

:set foldenable or in short, :set fen

To disable code folding:

:set nofoldenable or in short, :set nofen

Once you enable codefolding, you will have all the commands like zf,zo etc at your wish according to the setting of :set fdm=xxxx where typical values are expr,syntax,manual etc.

Loves Probability
  • 929
  • 1
  • 9
  • 15
3

Vim makes it amazingly hard to disable folding, especially when using vimdiff.
None of the above posted solutions worked for me, but this did (add to ~/.vimrc):

au WinEnter * set nofen
au WinLeave * set nofen
Magnus
  • 10,736
  • 5
  • 44
  • 57
1

Sorry, if I'm answering related question, but I found useful to display two files alongside with folding turned off with something like this:

vim "+set nofen" -O file1 file2
Denis Malinovsky
  • 5,840
  • 1
  • 22
  • 17
1

I set foldlevel=20, foldlevelstart=20 and I use foldmethod=syntax. It's help me work with correct folding in vim.

set nofoldenable disable folding but sometimes I need it

vim.wikia

idej
  • 5,034
  • 1
  • 11
  • 14