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

Write register contents to file

Expand Messages
  • howard Schwartz
    I would like to write the contents of a register to a file. Easy enough to grab the contents with let variable = getreg( a ). It would then be easy if :echo
    Message 1 of 5 , Apr 1, 2012
    • 0 Attachment
      I would like to write the contents of a register to a file. Easy enough to
      grab the contents with let variable = getreg('a'). It would then be easy
      if :echo variable > file worked like unix echo, but it doesnt. Any simple way
      to get contents of some register a into a file of my choosing? There is a
      writelist() function, but I do not know how to get the context of a register
      into a list.


      --
      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
    • Marcin Szamotulski
      ... What about ... or redir! /tmp/file echo variable redir end Best, Marcin -- You received this message from the vim_use maillist. Do not top-post! Type
      Message 2 of 5 , Apr 1, 2012
      • 0 Attachment
        On 02:00 Sun 01 Apr , howard Schwartz wrote:
        > I would like to write the contents of a register to a file. Easy enough to
        > grab the contents with let variable = getreg('a'). It would then be easy
        > if :echo variable > file worked like unix echo, but it doesnt. Any simple way
        > to get contents of some register a into a file of my choosing? There is a
        > writelist() function, but I do not know how to get the context of a register
        > into a list.
        >
        >
        > --
        > 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

        What about

        :exe "!echo ".variable." > /tmp/file"

        or

        redir! /tmp/file
        echo variable
        redir end

        Best,
        Marcin

        --
        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
      • John Beckett
        ... The following writes the default yank register to file x.tmp. ... John -- You received this message from the vim_use maillist. Do not top-post! Type your
        Message 3 of 5 , Apr 1, 2012
        • 0 Attachment
          howard Schwartz wrote:
          > I would like to write the contents of a register to a file.
          > Easy enough to grab the contents with let variable =
          > getreg('a'). It would then be easy if :echo variable > file
          > worked like unix echo, but it doesnt. Any simple way to get
          > contents of some register a into a file of my choosing? There is a
          > writelist() function, but I do not know how to get the
          > context of a register into a list.

          The following writes the default yank register to file x.tmp.

          :call writefile(split(@@, "\n", 1), 'x.tmp')

          John

          --
          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
        • Tim Chase
          ... Of the replies, don t use this one. It s susceptible to odd behaviors depending on the content of the variable (akin to SQL-injection) Go with Martin s
          Message 4 of 5 , Apr 1, 2012
          • 0 Attachment
            On 04/01/12 04:18, Marcin Szamotulski wrote:
            > What about
            >
            > :exe "!echo ".variable."> /tmp/file"

            Of the replies, don't use this one. It's susceptible to odd
            behaviors depending on the content of the variable (akin to
            SQL-injection)

            Go with Martin's :redir or John's writefile() instead.

            -tim


            --
            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
          • Tony Mechelynck
            ... As an alternative to all that s already been said, you could also do the ... which can be assigned to the {rhs} of a mapping. Pro: no need for an auxiliary
            Message 5 of 5 , Apr 1, 2012
            • 0 Attachment
              On 01/04/12 11:00, howard Schwartz wrote:
              > I would like to write the contents of a register to a file. Easy enough
              > to grab the contents with let variable = getreg('a'). It would then be easy
              > if :echo variable > file worked like unix echo, but it doesnt. Any
              > simple way to get contents of some register a into a file of my
              > choosing? There is a writelist() function, but I do not know how to get
              > the context of a register into a list.
              >
              >


              As an alternative to all that's already been said, you could also do the
              following:

              :new file | $put a | x

              which can be assigned to the {rhs} of a mapping.

              Pro: no need for an auxiliary variable

              Con: a new window is opened and closed. This, however, should happen too
              fast to be noticeable except that:
              - it requires a Vim compiled with +windows (rarely a problem)
              - if you're at the |hit-enter-prompt| (q.v.), it clears it.

              The snippet above appends the register at the end of the file (and after
              an empty line if the file didn't exist yet). Replace the dollar sign by
              a zero to insert at top, by a line number (see |[range]|) to insert
              after that line, or by nothing to insert after the current (usually
              latest used) line in the file.

              It is possible, with an if clause, to make the register contents the
              only contents of the file if it didn't yet exist. The details are left
              as an exercise to the reader. (Hint: see |filereadable()| and
              |filewriteable()|)

              See also |:execute| to read the file name (and path) and/or the register
              name from a variable.


              Best regards,
              Tony.
              --
              I've enjoyed just about as much of this as I can stand.

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