1

I have an HTML file with the CSS in the head. Usually I open the file in 2 tabs, so I can switch between the HTML and CSS easily, but I would also like to fold most of the code (especially the font-face declarations.) Unfortunately, whenever I open a second tab, every single fold is undone.

I can re-fold everything just fine afterwards, but if I open another tab it's all undone again.

I have code folding set up like this in my .vimrc:

 set foldmethod=indent " fold based on indent                          
 set nofoldenable " don't fold by default                            
 set foldlevel=1 " only close/open one foldlevel at a time             
 au BufWinLeave * silent! mkview " save view when closing buffer       
 au BufWinEnter * silent! loadview " load view when opening buffer
Jordan Acosta
  • 405
  • 4
  • 12

2 Answers2

1

First at all it's necessary to know that tabs are not the same thing that buffers. There are a lot a places where this is explained. You need to put this in your ".vimrc" file:

autocmd TabLeave * mkview

autocmd TabEnter * silent loadview

DrWaky
  • 1,165
  • 8
  • 7
0

Try doing :mkview in original buffer than move to same buffer in different tab and do :loadview. This should make the folds the same, which indicates that BufWinLeave and BufWinEnter are not the events you want. . . . Maybe it's also because foldenable is window-specific, in which case you also need to enable folds in the window on the new tab. . .

Also, you don't even need to bother with the mkview/loadview stuff if you just split (:split or :vsplit) the window in the current tab. That should give you two views of the same buffer with same fold setups. Once split the folds in each window will work independently. (:set winwidth=999; :set winminwidth=1 when using vertical splits will make current window automatically fill width of Vim's screen. . . )

Herbert Sitz
  • 21,858
  • 9
  • 50
  • 54
  • Splitting the current window is a solution, but I usually work with a browser open and taking up half the screen. Tabs let me maximize the screen space for each view. Using mkview/loadview manually does seem to work, although I'd hate to have to type all that out every time I open a tab. Is there another pair of events that replace BufWinEnter/BufWinLeave in my vimrc? – Jordan Acosta Jan 30 '12 at 22:58
  • I'm not sure, but I think you could leave BufWinEnter as is and change the `mkview` to work with `WinLeave` event. Seems like issue could also be that `nofoldenable` is your default, not sure if loadview automatically changes that or not, but you have to have `foldenable` set to have folds. – Herbert Sitz Jan 31 '12 at 00:49