To structure my .vimrc
a bit better I decided to create a custom fold expression in the file .vim/after/ftplugin/vim/folding.vim
which gets loaded automatically for my .vimrc
:
" The purpose of this script is to fold my .vimrc automatically.
" This file will be sourced automatically, each time a vim file is opened or
" created, see http://vimcasts.org/episodes/writing-a-custom-fold-expression/
" The function is called for each line of code, numbers represent fold level
function! VimFolds()
" get line contents
let thisline = getline(v:lnum)
if match(thisline, '^"""') >= 0
" tell Vim this line starts a level 2 fold, so Vim close all level 2 folds
" that came before it
return '>2'
else if match(thisline, '^""') >= 0
return '>1'
else
" tell Vim this line should have the same fold level as its predecessor
return '='
endif
endfunction
setlocal foldmethod=expr
setlocal foldexpr=VimFolds()
However, this code somehow does not really work for the first fold level (lines don't get folded), and the second fold level behaves weird as well (lines seem to be folded twice). I do not understand where the error is. I tried changing it to "##
and "#
, but this did not work either.
Tested on this .vimrc
:
"" plugins (managed via vim-plug)
" :PlugInstall/PlugUpdate [name], :PlugStatus, :PlugUpgrade to upgrade vim-plug
call plug#begin('~/.vim/plugged')
Plug 'altercation/vim-colors-solarized'
Plug 'tpope/vim-sensible'
Plug 'tpope/vim-commentary'
Plug 'tpope/vim-surround'
Plug 'tpope/vim-repeat'
Plug 'tpope/vim-unimpaired'
Plug 'scrooloose/syntastic'
" some more plugins here, not listed
call plug#end()
"" colorscheme settings
set background=dark
colorscheme solarized
""" testing a subfold
set relativenumber
set number " hybrid setting when both combined
" better line wrap
let &showbreak='↪ '
""" testing some more subfold
" enable spell checking for git commits
autocmd FileType gitcommit setlocal spell
" more code, not relevant
Where is my mistake?