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

Writing text to a file (invisibly)

Expand Messages
  • Tim Johnson
    I m using vim 7.10 on ubuntu 7.10 with python compiled in. I would like to be able to 1)select some text 2)write that text to a file in either w mode or a
    Message 1 of 6 , Jan 2, 2009
    • 0 Attachment
      I'm using vim 7.10 on ubuntu 7.10 with python compiled in.
      I would like to be able to

      1)select some text
      2)write that text to a file in either 'w' mode or 'a' mode
      I.E. 'w' = original contents of file are removed (truncated)
      something like emacs write-region =>
      (Write current region into specified file)
      'a' = select text is appended to original contents
      and do so as 'invisibly' as possible.

      I'd welcome comments on how to get started. I've mentioned the
      +python feature because I am a python programmer.
      thanks
      tim

      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Erik Hahn
      ... A bit hacky but it works: filter it throught tee. For w , enter !tee /foo/bar for a , enter !tee -a /foo/bar
      Message 2 of 6 , Jan 2, 2009
      • 0 Attachment
        On Fri, Jan 02, 2009 at 05:36:26PM -0900, Tim Johnson wrote:
        >
        > I'm using vim 7.10 on ubuntu 7.10 with python compiled in.
        > I would like to be able to
        >
        > 1)select some text
        > 2)write that text to a file in either 'w' mode or 'a' mode
        > I.E. 'w' = original contents of file are removed (truncated)
        > something like emacs write-region =>
        > (Write current region into specified file)
        > 'a' = select text is appended to original contents
        > and do so as 'invisibly' as possible.

        A bit hacky but it works: filter it throught tee.

        For 'w', enter !tee /foo/bar
        for 'a', enter !tee -a /foo/bar
      • Tony Mechelynck
        ... What do you call invisibly ? See ... e.g. :1,$d to empty the editfile before adding ... e.g. :$put (append register at the end of the editfile) ... with
        Message 3 of 6 , Jan 2, 2009
        • 0 Attachment
          On 03/01/09 03:36, Tim Johnson wrote:
          > I'm using vim 7.10 on ubuntu 7.10 with python compiled in.
          > I would like to be able to
          >
          > 1)select some text
          > 2)write that text to a file in either 'w' mode or 'a' mode
          > I.E. 'w' = original contents of file are removed (truncated)
          > something like emacs write-region =>
          > (Write current region into specified file)
          > 'a' = select text is appended to original contents
          > and do so as 'invisibly' as possible.
          >
          > I'd welcome comments on how to get started. I've mentioned the
          > +python feature because I am a python programmer.
          > thanks
          > tim

          What do you call "invisibly"?

          See
          :help :d
          e.g. :1,$d to empty the editfile before adding
          :help :put
          e.g. :$put (append register "
          at the end of the editfile)
          :help :redir
          with > or >> to a file.
          Use :redir! > file to erase (replace) an existing file
          :help :!
          (and pass a > or >> redirection to the shell)
          :help :w_f
          to write part of the current buffer (linewise)
          to an external file
          see also what comes below it, about overwriting (with !)
          and appending (with >>)
          :help :function
          :help :command
          to write your own functions and ex-commands

          None of the above requires python; but I recommend using at least a
          Normal build of Vim (otherwise you won't have expression evaluation and
          it makes script writing much harder -- no ifs, no whiles, no functions, etc.


          Best regards,
          Tony.
          --
          ARTHUR: Then who is your lord?
          WOMAN: We don't have a lord.
          ARTHUR: What?
          DENNIS: I told you. We're an anarcho-syndicalist commune. We take it in
          turns to act as a sort of executive officer for the week.
          The Quest for the Holy Grail (Monty
          Python)

          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        • Tom Link
          ... let lines = getline(from, to) ... For w , you could then use writefile(lines, filename), which wasn t mentioned before. ... Why not use it then? You could
          Message 4 of 6 , Jan 2, 2009
          • 0 Attachment
            > 1)select some text
            let lines = getline(from, to)

            > 2)write that text to a file in either 'w' mode or 'a' mode

            For 'w', you could then use writefile(lines, filename), which wasn't
            mentioned before.

            > +python feature because I am a python programmer.

            Why not use it then? You could do #1 in vim and #2 in python.

            --~--~---------~--~----~------------~-------~--~----~
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
            -~----------~----~----~----~------~----~------~--~---
          • Tim Johnson
            ... Minimum output visible to the user ..... Here s what I have tried: select text in visual mode y yank to register ... this command fails since
            Message 5 of 6 , Jan 3, 2009
            • 0 Attachment
              On Friday 02 January 2009, Tony Mechelynck wrote:

              > What do you call "invisibly"?
              Minimum output visible to the user .....
              Here's what I have tried:
              select text in visual mode
              y " yank to " register
              :let lines = @" " read register into variable
              :call writefile(lines,'targetfile')
              " this command fails since writefile() needs the first arg as list
              " so either another function is needed or a way to coerce `lines' to
              " a list

              > :help :function
              > :help :command
              >
              > to write your own functions and ex-commands
              To encapsulate this functionality into a function will have a number
              of applications for me. One application would be to emulate the emacs
              "scratch" buffer which allows the user to enter some elisp code, evaluate
              it and see the results read into the buffer
              this could be accomplished by 'targetfile' as source code as an argument
              to a call to a binary => exe ":r!language-binary targetfile"
              or given permission (and shebang) of course:
              exe ":r!targetfile"

              " simpler example follows:

              " I've alread written the following function:
              function! ComposeTime()
              exe "norm! 0v%y$"
              let arg = @"
              exe ":r !/home/http/run/baker/reb/compose-time.r " . arg
              exe "norm! kJ"
              endfunction
              output =>
              Line before:
              [[10:00 11:54][12:52 13:03][13:06 13:52][14:04 15:01]]
              line after:
              [[10:00 11:54][12:52 13:03][13:06 13:52][14:04 15:01]] 3:48
              :-) my hand-dandy timeclock!
              > None of the above requires python; but I recommend using at least a
              > Normal build of Vim (otherwise you won't have expression evaluation and
              > it makes script writing much harder -- no ifs, no whiles, no functions,
              > etc.
              I concur..
              Thanks to all for the input. I hope all finds this an interesting topic.
              All comments are contributing to my "vim education" :-)
              Sounds like coercing a string to a list would be the action that finishes
              this. I will look into that.
              Happy New Year!
              Tim

              --~--~---------~--~----~------------~-------~--~----~
              You received this message from the "vim_use" maillist.
              For more information, visit http://www.vim.org/maillist.php
              -~----------~----~----~----~------~----~------~--~---
            • Tim Johnson
              ... OK! ... ~tj~ The python sleeps tonight --~--~---------~--~----~------------~-------~--~----~ You received this message from the vim_use maillist.
              Message 6 of 6 , Jan 3, 2009
              • 0 Attachment
                On Saturday 03 January 2009, Tim Johnson wrote:

                > select text in visual mode
                > y " yank to " register
                >
                > :let lines = @" " read register into variable
                > :call writefile(lines,'targetfile')
                >
                > " this command fails since writefile() needs the first arg as list
                > " so either another function is needed or a way to coerce `lines' to
                > " a list
                OK!
                :call writefile(split(lines,'\n'),'targetfile')
                ~tj~
                """The python sleeps tonight"""

                --~--~---------~--~----~------------~-------~--~----~
                You received this message from the "vim_use" maillist.
                For more information, visit http://www.vim.org/maillist.php
                -~----------~----~----~----~------~----~------~--~---
              Your message has been successfully submitted and would be delivered to recipients shortly.