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

Re: delete lines from while loop

Expand Messages
  • Tom Link
    ... You might want to delete the lines in reverse order, i.e. from the bottom to the top. I also wonder if you could make use of the :g command. Regards, Tom
    Message 1 of 6 , Jun 1, 2010
    • 0 Attachment
      >    for linenum in range(a:firstline, a:lastline)

      You might want to delete the lines in reverse order, i.e. from the
      bottom to the top. I also wonder if you could make use of the :g
      command.

      Regards,
      Tom

      --
      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
    • Andrei Popescu
      ... Tried that, but couldn t find the right incantation, this is my first try at vimscript ;) ... Thanks a lot, :global is perfect! I m hitting another issue
      Message 2 of 6 , Jun 2, 2010
      • 0 Attachment
        On Ma, 01 iun 10, 21:32:01, Tom Link wrote:
        > >    for linenum in range(a:firstline, a:lastline)
        >
        > You might want to delete the lines in reverse order, i.e. from the
        > bottom to the top.

        Tried that, but couldn't find the right incantation, this is my first
        try at vimscript ;)

        > I also wonder if you could make use of the :g
        > command.

        Thanks a lot, :global is perfect! I'm hitting another issue now, with a
        part of code not included so far (hence the subject change). Now I have:

        fu! <SID>RemoveFuzzy() range
        execute ':''<,''>global/^#,\sfuzzy$\|^#|\s.*".*"$/d'
        for linenum in range(a:firstline, a:lastline)
        let line = getline(linenum)
        if line =~ '^#,\(.*,\)\=\s*fuzzy'
        call setline(linenum, substitute(line, ',\s*fuzzy', '', ""))
        endif
        endfor
        endf

        I tried (no pun intended) to replace the 'for' loop with:

        try
        execute ':''<,''>global/^#,\(.*,\)\=\s*fuzzy/s/,\s*fuzzy//'
        catch /^Vim\%((\a\+)\)\=:E486/ " based on example from :help catch
        endtry

        but I still get:

        E486: Pattern not found: ^#,\(.*,\)\=\s*fuzzy

        I can work around it by reversing the order, let the substitute global
        do its work and then remove any '^#$' lines left behind in the deleting
        global, but according to :help it should work.

        Regards,
        Andrei
        --
        http://nuvreauspam.ro/2010/05/4-neticheta-pe-mail/
      • Andrei Popescu
        [sorry for replying to myself] ... Much simpler: execute : s/, s*fuzzy//e but I m still curious why the :catch doesn t work... Regards, Andrei --
        Message 3 of 6 , Jun 2, 2010
        • 0 Attachment
          [sorry for replying to myself]

          On Mi, 02 iun 10, 11:32:03, Andrei Popescu wrote:
          >
          > fu! <SID>RemoveFuzzy() range
          > execute ':''<,''>global/^#,\sfuzzy$\|^#|\s.*".*"$/d'
          > for linenum in range(a:firstline, a:lastline)
          > let line = getline(linenum)
          > if line =~ '^#,\(.*,\)\=\s*fuzzy'
          > call setline(linenum, substitute(line, ',\s*fuzzy', '', ""))
          > endif
          > endfor
          > endf
          >
          > I tried (no pun intended) to replace the 'for' loop with:
          >
          > try
          > execute ':''<,''>global/^#,\(.*,\)\=\s*fuzzy/s/,\s*fuzzy//'
          > catch /^Vim\%((\a\+)\)\=:E486/ " based on example from :help catch
          > endtry
          >
          > but I still get:
          >
          > E486: Pattern not found: ^#,\(.*,\)\=\s*fuzzy

          Much simpler:

          execute ':''<,''>s/,\s*fuzzy//e'

          but I'm still curious why the :catch doesn't work...

          Regards,
          Andrei
          --
          http://nuvreauspam.ro/2010/05/4-neticheta-pe-mail/
        Your message has been successfully submitted and would be delivered to recipients shortly.