[prev in list] [next in list] [prev in thread] [next in thread] 

List:       vim
Subject:    Re: automatically opening folds
From:       Bram Moolenaar <Bram () moolenaar ! net>
Date:       2002-12-29 21:39:32
[Download RAW message or body]


Emmanuel Touzery wrote:

> When I start vim, the cursor is automatically positionned on the position it 
> was last time i used the file (macro taken in 
> /usr/local/share/vim/vim60/vimrc_example.vim). However, if I was in a
> fold, i have each time to enter zo to open the fold. Is there a way
> that vim could do this for me? I tried to modify the macro but
> couldn't get it to work.
> 
> emmanuel
> 
> PS: the full macro I use for now is :
> if has("autocmd")
>         autocmd BufReadPost *
>         \ if line("'\"") > 0 && line("'\"") <= line("$") |
>         \   exe "normal g`\"" | 
>         \ endif
> endif " has("autocmd")

I assume the 'foldmethod' is being set by a modeline.  In that case you
need to execute the "zv" command with a BufWinEnter autocommand, but
only when the `" command was used.  That's because the BufReadPost event
is triggered before the modelines are processed.

This code should do it:

  autocmd BufReadPost *
    \ if line("'\"") > 0 && line("'\"") <= line("$") |
    \   exe "normal g`\"" |
    \   let b:doopenfold = 1 |
    \ endif
  " Need to postpone using "zv" until after reading the modelines.
  autocmd BufWinEnter *
    \ if exists("b:doopenfold") |
    \   unlet b:doopenfold |
    \   exe "normal zv" |
    \ endif

-- 
    "You mean there really is an answer?"
    "Yes! But you're not going to like it!"
    "Oh do please tell us!"
    "You're really not going to like it!"
    "but we MUST know - tell us"
    "Alright, the answer is...."
    "yes..."
    "... is ..."
    "yes... come on!"
    "is 42!"
		(Douglas Adams - The Hitchhiker's Guide to the Galaxy)

 ///  Bram Moolenaar -- Bram@moolenaar.net -- http://www.moolenaar.net  \\\
///          Creator of Vim - Vi IMproved -- http://www.vim.org          \\\
\\\           Project leader for A-A-P -- http://www.a-a-p.org           ///
 \\\ Lord Of The Rings helps Uganda - http://iccf-holland.org/lotr.html ///
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic