2

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?

4

Make the else if an elseif.

Also I find it usually simpler to give the variable v:lnum as parameter to the foldfunction. This makes it easier to debug the function.

Some hints: You can use :set debug=msg to see error messages and you can use :breakadd func VimFolds to start the debugger when the function is executed. Setting :set foldcolumn=5 gives a visual indication of the result of the folding expression. This is also often useful.

| improve this answer | |
  • 2
    Wow, crazy! I never knew about the debugging mode! Thanks a bunch, this is going to be really useful! :D – Karl Yngve Lervåg Nov 19 '15 at 7:11
  • 2
    This is the kind of mistake that is usually the hardest to find for me, especially since the syntax highlighting did look ok. Thanks a lot, especially for your additional hints (I was not aware of the built-in debugger either). – cbaumhardt Nov 19 '15 at 11:12
  • 1
    I agree with you, the hints given by Christian Brabandt have a great value, I think I will use them a lot from now on. I temporarily copied your code inside my vimrc and the offending part else if was colored in red. When you don't remember how to type an instruction in vimscript, or the name of a function, here's another tip : type the beginning of the word (here for example el), then hit Ctrl-x Ctrl-o. It should trigger omnicompletion which will give you a few suggestions inside a popup menu. You can cycle through the suggestions with Ctrl-n and Ctrl-p. – saginaw Nov 19 '15 at 11:45
  • @saginaw: Do you say that you were able to see that mistake just by looking at the colors? For me there is no difference in colors for elseif and else if. Do you have an additional syntax file for vimscript? Or do you use a linter for VimL with Syntastic? – cbaumhardt Nov 19 '15 at 13:47
  • 1
    I've done it in my vimrc, the highlight group is vimElseIfErr which is linked to (inherits its attributes from) the highlight group Error. However, in the folding.vim file, the highlight group used to colorize elseif is vimNotFunc which is linked to Statement. I don't know why the vimNotFunc group is used instead of vimElseIfErr. If you want to know more about syntax highlighting, you could read the chapters 45 to 47 of this site : learnvimscriptthehardway.stevelosh.com/chapters/45.html Otherwise you should probably update your question or open another topic. – saginaw Nov 19 '15 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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