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

Feature request: Need a function to read a file into a variable

Expand Messages
  • Yegappan Lakshmanan
    Hi all, Currently to read a file into a variable from a Vim function, we have to use ... or, we have to use let var = system( cat /path/to/file ) As this one
    Message 1 of 4 , Jun 27, 2002
    • 0 Attachment
      Hi all,

      Currently to read a file into a variable from a Vim function, we have to use
      either the following set of commands:

      :new
      :read /path/to/file
      :%yank
      :close!
      :let var = @"

      or, we have to use

      let var = system("cat /path/to/file")

      As this one uses the 'cat' command, this will work only in Unix. We have to
      replace 'cat' with 'type' for MS-Windows.

      Both of the above approaches look kludgy to me. We need a readfile()
      function which returns the contents of the file, so that we can use

      let var = readfile("/path/to/file")

      Similary, we need a function to write the contents of a variable into
      a file:

      call writefile("/path/to/file", var)

      Currently to write the contents of a variable to a file, we have to use:

      :redir! > /path/to/file
      :silent echo var
      :redir END

      The above two functions will help in writing some good Vim plugins.

      - Yegappan


      __________________________________________________
      Do You Yahoo!?
      Yahoo! - Official partner of 2002 FIFA World Cup
      http://fifaworldcup.yahoo.com
    • Bram Moolenaar
      ... Or open a new window, put the variable there and write the buffer. You can write a Vim function to do this. Why would it need to be a built-in function?
      Message 2 of 4 , Jun 27, 2002
      • 0 Attachment
        Yegappan Lakshmanan wrote:

        > Currently to read a file into a variable from a Vim function, we have to use
        > either the following set of commands:
        >
        > :new
        > :read /path/to/file
        > :%yank
        > :close!
        > :let var = @"
        >
        > or, we have to use
        >
        > let var = system("cat /path/to/file")
        >
        > As this one uses the 'cat' command, this will work only in Unix. We have to
        > replace 'cat' with 'type' for MS-Windows.
        >
        > Both of the above approaches look kludgy to me. We need a readfile()
        > function which returns the contents of the file, so that we can use
        >
        > let var = readfile("/path/to/file")
        >
        > Similary, we need a function to write the contents of a variable into
        > a file:
        >
        > call writefile("/path/to/file", var)
        >
        > Currently to write the contents of a variable to a file, we have to use:
        >
        > :redir! > /path/to/file
        > :silent echo var
        > :redir END

        Or open a new window, put the variable there and write the buffer.

        You can write a Vim function to do this. Why would it need to be a
        built-in function? This would soon go in the direction of a full script
        language, and that's not a goal for Vim.

        --
        ARTHUR: I did say sorry about the `old woman,' but from the behind you
        looked--
        DENNIS: What I object to is you automatically treat me like an inferior!
        ARTHUR: Well, I AM king...
        The Quest for the Holy Grail (Monty Python)

        /// Bram Moolenaar -- Bram@... -- http://www.moolenaar.net \\\
        /// Creator of Vim -- http://vim.sf.net -- ftp://ftp.vim.org/pub/vim \\\
        \\\ Project leader for A-A-P -- http://www.a-a-p.org ///
        \\\ Help me helping AIDS orphans in Uganda - http://iccf-holland.org ///
      • Kontra, Gergely
        ... Well, almost true, but this can screw up your windows positions, if the cursor is at the bottom right of a window. Gergo +-[Kontra, Gergely @ Budapest
        Message 3 of 4 , Jul 1, 2002
        • 0 Attachment
          >You can write a Vim function to do this. Why would it need to be a
          >built-in function? This would soon go in the direction of a full script
          >language, and that's not a goal for Vim.

          Well, almost true, but this can screw up your windows positions, if the
          cursor is at the bottom right of a window.

          Gergo


          +-[Kontra, Gergely @ Budapest University of Technology and Economics]-+
          | Email: kgergely@..., kgergely@... |
          | URL: turul.eet.bme.hu/~kgergely Mobile: (+36 20) 356 9656 |
          +-------"Olyan langesz vagyok, hogy poroltoval kellene jarnom!"-------+
          .
          Magyar php mirror es magyar php dokumentacio: http://hu.php.net
        • Benji Fisher
          ... I think what we really need is an invisible, temporary buffer, so we can do all sorts of fun stuff from scripts without worrying about messing up the
          Message 4 of 4 , Jul 1, 2002
          • 0 Attachment
            On Monday, July 1, 2002, at 09:05 AM, Kontra, Gergely wrote:

            >> You can write a Vim function to do this. Why would it need to be a
            >> built-in function? This would soon go in the direction of a full
            >> script
            >> language, and that's not a goal for Vim.
            >
            > Well, almost true, but this can screw up your windows positions, if the
            > cursor is at the bottom right of a window.

            I think what we really need is an invisible, temporary buffer, so
            we can do all sorts of fun stuff from scripts without worrying about
            messing up the window arrangement.

            --Benji Fisher
          Your message has been successfully submitted and would be delivered to recipients shortly.