Loading ...
Sorry, an error occurred while loading the content.

Fold all blocks without TODO in vim notes file

Expand Messages
  • Sepp Tannhuber
    Dear all, I have the following vim notes file: ~  notes ~ ~  #2012-04-12 ~     ◦ DONE beer ~  ~  #2012-04-13 ~     ◦ DONE beer ~     ◦ DONE
    Message 1 of 5 , Jul 3, 2012
    • 0 Attachment
      Dear all,

      I have the following vim notes file:

      ~  notes
      ~
      ~  #2012-04-12
      ~     ◦ DONE beer

      ~  #2012-04-13
      ~     ◦ DONE beer
      ~     ◦ DONE another beer
      ~
      ~  #2012-06-30
      ~     ◦ DONE beer
      ~     ◦ TODO another beer

      ~  #2012-07-02
      ~     ◦ TODO beer


      If I start vim with
      ~ vi notes -c 'set foldenable' -c 'set foldlevel=0'
      all blocks beginning with a hash mark are folded.
      But I would like to have only the blocks folded
      that do not have a TODO item in their list. In the
      given example only the first two blocks 2012-04-12

      and 2012-04-13 should be folded because they are
      done completely.

      How can this be done?


      Best regards
      Joseph

      --
      You received this message from the "vim_use" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php
    • Salman Halim
      ... Quick answer that comes to me off the top of my head: use a custom fold expression. (See :help foldexpr ) Basically, you want something that does
      Message 2 of 5 , Jul 3, 2012
      • 0 Attachment
        On Tue, Jul 3, 2012 at 2:27 PM, Sepp Tannhuber <sepp.tannhuber@...> wrote:
        > Dear all,
        >
        > I have the following vim notes file:
        >
        > ~ notes
        > ~
        > ~ #2012-04-12
        > ~ ◦ DONE beer
        > ~
        > ~ #2012-04-13
        > ~ ◦ DONE beer
        > ~ ◦ DONE another beer
        > ~
        > ~ #2012-06-30
        > ~ ◦ DONE beer
        > ~ ◦ TODO another beer
        > ~
        > ~ #2012-07-02
        > ~ ◦ TODO beer
        >
        >
        > If I start vim with
        > ~ vi notes -c 'set foldenable' -c 'set foldlevel=0'
        > all blocks beginning with a hash mark are folded.
        > But I would like to have only the blocks folded
        > that do not have a TODO item in their list. In the
        > given example only the first two blocks 2012-04-12
        >
        > and 2012-04-13 should be folded because they are
        > done completely.
        >
        > How can this be done?
        >
        >
        > Best regards
        > Joseph

        Quick answer that comes to me off the top of my head: use a custom
        fold expression. (See :help 'foldexpr')

        Basically, you want something that does something like this:

        if indent of current line is 0 (probably a date line, not a DONE or
        TODO line) or line is entirely whitespace
        return 0 (no fold)
        else
        check if current line starts with TODO
        return 0
        else
        go up, checking lines until indent is 0 (or is a whitespace
        line); if any line has a TODO, return 0
        go down from current line, checking the same

        Finally, if we got this far, we're not a TODO block or on an empty or
        date line; return 1 to fold it.

        Salman

        --
        You received this message from the "vim_use" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php
      • Sepp Tannhuber
        Hi Salman, thanks for answering! ... With this method I can unfold block by block with hitting . Is it possible to automate this on startup so that vi
        Message 3 of 5 , Jul 3, 2012
        • 0 Attachment
          Hi Salman,

          thanks for answering!

          For now I use a simple key mapping:
          : map <C-f> <Esc>/TODO<CR>zo

          With this method I can unfold block by block with hitting <C-f>.
          Is it possible to automate this on startup so that vi hits <C-f> until it reaches
          end of file?

          Cheers
          Joseph:x

          --
          You received this message from the "vim_use" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php
        • Salman Halim
          ... However, this is a one-time that will happen automatically when the ... This should work for both insert, visual and normal mode. In insert mode, it will
          Message 4 of 5 , Jul 3, 2012
          • 0 Attachment
            On Tue, Jul 3, 2012 at 6:08 PM, Sepp Tannhuber <sepp.tannhuber@...> wrote:
            > Hi Salman,
            >
            > thanks for answering!
            >
            > For now I use a simple key mapping:
            > : map <C-f> <Esc>/TODO<CR>zo
            >
            > With this method I can unfold block by block with hitting <C-f>.
            > Is it possible to automate this on startup so that vi hits <C-f> until it reaches
            > end of file?
            >
            > Cheers
            > Joseph:x

            You could just do something like:

            :au BufRead <TheFileName> g/TODO/foldopen!

            However, this is a one-time that will happen automatically when the
            file is opened; for a mapping, I would do:

            :map <c-f> :g/TODO/foldopen!
            :imap <c-f> <c-o><c-f>

            This should work for both insert, visual and normal mode. In insert
            mode, it will return you to insert mode after the mapping and, in
            visual mode, it will limit the effect to the visually selected region.

            For help on autocommands, try :help :au, :help BufRead, :help :g and
            :help :foldopen.

            Salman

            --
            You received this message from the "vim_use" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php
          • Sepp Tannhuber
            Hi Salman, thank you very much! I did it with your help. I added your key mappings ... to my ~/.vim/ftplugin/notes.vim and made a keyboard shortcut ... for my
            Message 5 of 5 , Jul 4, 2012
            • 0 Attachment
              Hi Salman,

              thank you very much! I did it with your help. I added your key mappings
              > map <C-f> :g/TODO/foldopen!<CR>
              > imap <C-f> <C-o><C-f>
              to my ~/.vim/ftplugin/notes.vim and made a keyboard shortcut
              > vi notes -c 'set foldenable' -c 'set foldlevel=0' -c 'g/TODO/foldopen!'
              for my window manager. This does exactly what I want.


              Thank you
              Sepp

              --
              You received this message from the "vim_use" maillist.
              Do not top-post! Type your reply below the text you are replying to.
              For more information, visit http://www.vim.org/maillist.php
            Your message has been successfully submitted and would be delivered to recipients shortly.