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

Re: global and yank

Expand Messages
  • Erik Warendorph
    ... Here s another variation. Of course, the solutions above will do quite nicely in most cases, especially when typed in manually by the user. But if this
    Message 1 of 5 , Nov 29, 2002
    • 0 Attachment
      * * Scott LaBounty <slabounty@...> [2002-11-27 19:09:44 +0100]:
      > > let @a=""
      > > g/pattern/normal "AY

      * Tim Chase <gumnos@...> [2002-11-27 19:22:38 +0100]:
      > let @a=""
      > g/pattern/y A

      Here's another variation. Of course, the solutions above will
      do quite nicely in most cases, especially when typed in
      manually by the user. But if this is to be used in a script or
      a mapping, the following may be a little more robust:

      :mark '
      :let @a = ""
      :g/pattern/sil y A
      :let @a = substitute(@a, "^\n", "", "")
      :norm ``

      The first and last commands (mark ' and norm ``) are used to
      keep the cursor at the same position after the commands have
      been executed (otherwise the g// command will move it to the
      last line matching the pattern).

      The sil (silent) command is used to avoid the "-- More --" and
      "Hit ENTER or type command to continue" messages. (Well, these
      are actually displayed because I've set the 'report' option to
      0, the default is 2).

      The substitute() is necessary to get rid of the newline which
      is inserted at the start of the a register when the first
      matching line is append-yanked.

      By the way, does anyone know of a way to avoid this leading
      newline when appending? Is this a bug in vim? When appending
      a line to a register which only contains an empty string, I
      don't think it's expected behaviour to first insert a newline.

      Another strange thing is that if the register hasn't been used
      before the line-append-yanking (that is, if it's not listed by
      the ;reg command), the newline is *not* inserted at the start.
      So if it was possible to set a register "unused", it could be
      solved that way. I've tried

      :unlet @a

      but that gives the error message

      E108: No such variable: "@a"

      And interestingly the command

      :unlet! @a

      makes vim freeze completely, and I have to kill it.

      --
      Erik Warendorph <erik.warendorph@...>
    Your message has been successfully submitted and would be delivered to recipients shortly.