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

Re: vim: format each long lines and insert just one break afterward

Expand Messages
  • ping
    ... hi tim: thanks for the improvement, I tested and your commands works. I have 2 questions here: 1) about the magic put_ I couldn t understand, how come
    Message 1 of 9 , May 18, 2013
      On 5/18/2013 11:13 PM, Tim Chase wrote:
      > On 2013-05-18 20:22, ping wrote:
      >> 1) :'<,'>g/.*/exec "normal! o\<esc>"
      >> unconditionally insert a new line after each line (this may bring a
      >> lot of empty lines , if the original book already contains empty
      >> lines)
      > Any line matching ".*" could be written as just "^", and the
      > "normal..." bit can be done with an ex command, making a simpler
      > version:
      >
      > :'<,'>g/^/put_
      >
      > (put the null "_" register)
      >
      >> 2) format all each line seperately.
      >> :'<,'>g/.*/exec "normal! gqq"
      >> this is different than gggqG (this will mess up everything :D )
      > and again here, using "^" instead of ".*"
      >
      > -tim
      >
      >
      >

      hi tim:
      thanks for the improvement, I tested and your commands works.

      I have 2 questions here:
      1) about the magic "put_"
      I couldn't understand, how come "put" a null register "_" will put a new
      line?
      my understanding is if it's a null then it shouldn't do anything "in
      theory" right...
      what's the idea behind this?

      :[line]pu[t] [x] Put the text [from register x] after [line] (default
      current line). This always works linewise, thus
      this command can be used to put a yanked block
      as new
      lines.
      The cursor is left on the first non-blank in
      the last
      new line.
      The register can also be '=' followed by an
      optional
      expression. The expression continues until the
      end of
      the command. You need to escape the '|' and '"'
      characters to prevent them from terminating the
      command. Example: >
      :put ='path' . \",/test\"
      < If there is no expression after '=', Vim uses the
      previous expression. You can see it with ":dis =".

      2) now with the new way I have:

      '<,'>g/^/put_
      '<,'>g/^/exec "normal! gqq"
      '<,'>s/^\s*\n\n\+/\r/

      since I'm satisfied with this solution and I want to make it an easier
      bind, so I make this:

      function! FormatBook(...) range
      exec a:firstline . "," . a:lastline . "g/^/put_"
      exec a:firstline . "," . a:lastline . "g/^/exec " . "normal! gqq"
      exec a:firstline . "," . a:lastline . "s/^\s*\n\n\+/\r/"
      endf

      com! -range=% -nargs=* FormatBook :<line1>,<line2>call
      FormatBook(<f-args>)
      map ,bf :FormatBook<CR>

      but when I tested it report errors:

      14 more lines
      Error detected while processing function FormatBook:
      line 2:
      E121: Undefined variable: normal
      E15: Invalid expression: normal! gqq
      line 3:
      E486: Pattern not found: ^s*^@^@+

      seems something wrong with 2nd line:
      exec a:firstline . "," . a:lastline . "g/^/exec " . "normal! gqq"

      I know this is weired , but how to generalize this to make it work with
      range via a simple key map?

      thanks!

      --
      --
      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

      ---
      You received this message because you are subscribed to the Google Groups "vim_use" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • Gary Johnson
      ... Isn t that explained in those two lines? _ is an empty register. :put _ puts the contents of that register in a new line ( linewise ) after the current
      Message 2 of 9 , May 19, 2013
        On 2013-05-19, ping wrote:
        > On 5/18/2013 11:13 PM, Tim Chase wrote:
        > >On 2013-05-18 20:22, ping wrote:
        > >>1) :'<,'>g/.*/exec "normal! o\<esc>"
        > >>unconditionally insert a new line after each line (this may bring a
        > >>lot of empty lines , if the original book already contains empty
        > >>lines)
        > >Any line matching ".*" could be written as just "^", and the
        > >"normal..." bit can be done with an ex command, making a simpler
        > >version:
        > >
        > > :'<,'>g/^/put_
        > >
        > >(put the null "_" register)
        > >
        > >>2) format all each line seperately.
        > >>:'<,'>g/.*/exec "normal! gqq"
        > >>this is different than gggqG (this will mess up everything :D )
        > >and again here, using "^" instead of ".*"
        > >
        > >-tim
        > >
        > >
        > >
        >
        > hi tim:
        > thanks for the improvement, I tested and your commands works.
        >
        > I have 2 questions here:
        > 1) about the magic "put_"
        > I couldn't understand, how come "put" a null register "_" will put a
        > new line?
        > my understanding is if it's a null then it shouldn't do anything "in
        > theory" right...
        > what's the idea behind this?
        >
        > :[line]pu[t] [x] Put the text [from register x] after [line] (default
        > current line). This always works linewise, thus
        > this command can be used to put a yanked
        > block as new lines.

        Isn't that explained in those two lines? _ is an empty register.
        ":put _" puts the contents of that register in a new line
        ("linewise") after the current line. Hence, ":put _" creates a new,
        empty line.

        > 2) now with the new way I have:
        >
        > '<,'>g/^/put_
        > '<,'>g/^/exec "normal! gqq"
        > '<,'>s/^\s*\n\n\+/\r/
        >
        > since I'm satisfied with this solution and I want to make it an
        > easier bind, so I make this:
        >
        > function! FormatBook(...) range
        > exec a:firstline . "," . a:lastline . "g/^/put_"
        > exec a:firstline . "," . a:lastline . "g/^/exec " . "normal! gqq"
        > exec a:firstline . "," . a:lastline . "s/^\s*\n\n\+/\r/"
        > endf
        >
        > com! -range=% -nargs=* FormatBook :<line1>,<line2>call
        > FormatBook(<f-args>)
        > map ,bf :FormatBook<CR>
        >
        > but when I tested it report errors:
        >
        > 14 more lines
        > Error detected while processing function FormatBook:
        > line 2:
        > E121: Undefined variable: normal
        > E15: Invalid expression: normal! gqq
        > line 3:
        > E486: Pattern not found: ^s*^@^@+
        >
        > seems something wrong with 2nd line:
        > exec a:firstline . "," . a:lastline . "g/^/exec " . "normal! gqq"
        >
        > I know this is weired , but how to generalize this to make it work
        > with range via a simple key map?

        I don't understand why you're going through all these gyrations to
        perform what appears to be a simple task, as I answered originally.
        If you want a set of lines to be formatted as paragraphs, with each
        line as its own paragraph and separated by single blank lines, all
        you have to do is first separate each line from the others by single
        blank lines, which can be done by this:

        :s/\n\+/\r\r/

        with any range that you like. If some of your blank lines are not
        empty (i.e., contain spaces), use this instead:

        :s/\(\s*\n\)\+/\r\r/

        Once all your paragraph lines are separated by blank lines, there is
        no need to format each one individually; you can reformat them all
        at once. Further, if you previously selected the lines visually,
        you can reformat that same visual region with this normal-mode
        command:

        gvgq

        Putting those together with a visual-mode mapping yields this:

        :vnoremap ,bf :s/\(\s*\n\)\+/\r\r/<CR>gvgq

        I don't care what solution you finally adopt. Vim offers many ways
        to solve the same problem. I do care that you understand what
        you're doing. I also care that I understand what I'm doing, so when
        a solution I propose doesn't seem to work, I like to understand why.

        Regards,
        Gary

        --
        --
        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

        ---
        You received this message because you are subscribed to the Google Groups "vim_use" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • ping
        this look much simpler, quick and works great. good to learn! ... still, just for a learning purpose, I m curious about what s wrong with the following
        Message 3 of 9 , May 20, 2013
          this look much simpler, quick and works great. good to learn!

          On 05/19/2013 02:30 PM, Gary Johnson wrote:

          > Putting those together with a visual-mode mapping yields this:
          >
          > :vnoremap ,bf :s/\(\s*\n\)\+/\r\r/<CR>gvgq


          still, just for a learning purpose, I'm curious about what's wrong with
          the following script?

          since I'm satisfied with this solution and I want to make it an
          easier bind, so I make this:

          function! FormatBook(...) range
          exec a:firstline . "," . a:lastline . "g/^/put_"
          exec a:firstline . "," . a:lastline . "g/^/exec " . "normal! gqq"
          exec a:firstline . "," . a:lastline . "s/^\s*\n\n\+/\r/"
          endf

          com! -range=% -nargs=* FormatBook :<line1>,<line2>call
          FormatBook(<f-args>)
          map ,bf :FormatBook<CR>


          specifically, I want to learn what's the right way to re-write the
          following line in a ranged func ?

          exec a:firstline . "," . a:lastline . "g/^/exec " . "normal! gqq"



          thanks!

          regards
          ping

          --
          --
          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

          ---
          You received this message because you are subscribed to the Google Groups "vim_use" group.
          To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
          For more options, visit https://groups.google.com/groups/opt_out.
        • Gary Johnson
          ... The action of the first exec is to expand and execute its arguments, which in this case is a single argument, the string a:firstline . , . a:lastline .
          Message 4 of 9 , May 20, 2013
            On 2013-05-20, ping wrote:
            > this look much simpler, quick and works great. good to learn!
            >
            > On 05/19/2013 02:30 PM, Gary Johnson wrote:
            >
            > >Putting those together with a visual-mode mapping yields this:
            > >
            > > :vnoremap ,bf :s/\(\s*\n\)\+/\r\r/<CR>gvgq
            >
            >
            > still, just for a learning purpose, I'm curious about what's wrong
            > with the following script?
            >
            > since I'm satisfied with this solution and I want to make it an
            > easier bind, so I make this:
            >
            > function! FormatBook(...) range
            > exec a:firstline . "," . a:lastline . "g/^/put_"
            > exec a:firstline . "," . a:lastline . "g/^/exec " . "normal! gqq"
            > exec a:firstline . "," . a:lastline . "s/^\s*\n\n\+/\r/"
            > endf
            >
            > com! -range=% -nargs=* FormatBook :<line1>,<line2>call
            > FormatBook(<f-args>)
            > map ,bf :FormatBook<CR>
            >
            >
            > specifically, I want to learn what's the right way to re-write the
            > following line in a ranged func ?
            >
            > exec a:firstline . "," . a:lastline . "g/^/exec " . "normal! gqq"

            The action of the first exec is to expand and execute its arguments,
            which in this case is a single argument, the string

            a:firstline . "," . a:lastline . "g/^/exec " . "normal! gqq"

            Assuming for sake of a concrete example a firstline of 123 and a
            lastline of 456, that string expands to this:

            123,456g/^/exec normal! gqq

            Note that this second exec sees "normal!" and "gqq" not as strings
            but as variables. That is what is causing the error message you
            reported previously.

            You don't need that second "exec" at all, so a better way to write
            that command is this:

            a:firstline . "," . a:lastline . "g/^/normal! gqq"

            Regards,
            Gary

            --
            --
            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

            ---
            You received this message because you are subscribed to the Google Groups "vim_use" group.
            To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
            For more options, visit https://groups.google.com/groups/opt_out.
          • ping
            ... I Got it now, thanks! -- -- You received this message from the vim_use maillist. Do not top-post! Type your reply below the text you are replying to. For
            Message 5 of 9 , May 20, 2013
              On 05/20/2013 01:03 PM, Gary Johnson wrote:
              > The action of the first exec is to expand and execute its arguments,
              > which in this case is a single argument, the string
              >
              > a:firstline . "," . a:lastline . "g/^/exec " . "normal! gqq"
              >
              > Assuming for sake of a concrete example a firstline of 123 and a
              > lastline of 456, that string expands to this:
              >
              > 123,456g/^/exec normal! gqq
              >
              > Note that this second exec sees "normal!" and "gqq" not as strings
              > but as variables. That is what is causing the error message you
              > reported previously.
              >
              > You don't need that second "exec" at all, so a better way to write
              > that command is this:
              >
              > a:firstline . "," . a:lastline . "g/^/normal! gqq"
              I Got it now, thanks!

              --
              --
              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

              ---
              You received this message because you are subscribed to the Google Groups "vim_use" group.
              To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
              For more options, visit https://groups.google.com/groups/opt_out.
            Your message has been successfully submitted and would be delivered to recipients shortly.