Search code examples
vimfoldingmodeline

Activate VIM folding markers via modeline


I'm trying to figure out why folding doesn't work with multi-line comments and I have no ideas.

Let's consider such example:

#include <stdio.h>
#include <stdlib.h>

void
panic(void) /* {{{ */
{
    abort();
}
/* }}} */

void
say_hello(void) /* {{{ */
{
    printf("Hello, World \n");  
}
/* }}} */

void
say_goodbye(void) /* {{{ */
{
    printf("Good Bye, World \n");
    panic();
}
/* }}} */

int
main(void) /* {{{ */
{
    void (*message) (void);

    message = say_goodbye;
    message();

    message = say_hello;
    message();

    return 0;
}
/* }}} */

The following modelines works as expected. While opening the file, I see that all the code is folded:

/* vim600: set noet sw=4 ts=4 fdm=marker : */

and

// vim600: noet sw=4 ts=4 fdm=marker

enter image description here

The command :verbose set syntax filetype foldmethod foldexpr

shows expected output

  syntax=c
        Last set from /usr/share/vim/vim74/syntax/syntax.vim
  filetype=c
        Last set from /usr/share/vim/vim74/filetype.vim
  foldmethod=marker
        Last set from modeline
  foldexpr=0

However, any variations with multi-line comments do not give the desired result. For example:

/*
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */

or even

/* Modeline for ViM {{{
 * vim: noet:sw=4:ts=4
 * vim600: noet:sw=4:ts=4:fdm=marker
 * }}} */

Will not automatically fold the code when opening a file:

  syntax=c
        Last set from /usr/share/vim/vim74/syntax/syntax.vim
  filetype=c
        Last set from /usr/share/vim/vim74/filetype.vim
  foldmethod=manual
  foldexpr=0

Solution

  • Check the setting of modelines. Vim searches the first or last modelines lines for a modeline. With modelines set to 1 the modeline has to be either on the first or last line of the file.

    The default value of modelines is 5. This would work for your examples.

    See :help modelines