2

I've got automatic folding enabled in my file with the following modeline:

" vim:foldmethod=marker:foldlevel=0

I also have an autocmd like this to place the cursor where I left it:

if has("autocmd")
    au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$")
          \| exe "normal! g'\"" | endif
endif

The problem is that if I left the cursor in the middle of a fold then my auto command will not work as desired.

What can I do to unfold the fold under the cursor on read, preferably using modeline?

I've tried adding normal zv to my autocmd but it didn't work.

3

You can only set options with the modeline. You can't run Vimscript with it. This is for security reasons, because running code from random text files is not very good security.

The reason that running normal zv from the autocommand doesn't work is that the BufReadPost autocommand is run before the modeline is processed. From :help BufReadPost:

BufRead or BufReadPost          When starting to edit a new buffer, after
                                reading the file into the buffer, before
                                executing the modelines.  See BufWinEnter
                                for when you need to do something after
                                processing the modelines.

Luckily, it also provides a hint on how to proceed :-) Adding:

au BufWinEnter * normal! zv

Seems to work well.

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.