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

Naming a temporary buffer ?

Expand Messages
  • "Peter Valdemar Mørch (Lists)"
    I have a script where I want to show some output to the user. I want the window to have a *meaningful* name ( DiffOutput ), and want the user to be able to
    Message 1 of 11 , Sep 8, 2008
      I have a script where I want to show some output to the user. I want the
      window to have a *meaningful* name ("DiffOutput"), and want the user to
      be able to switch back and forth between his files and the DiffOutput
      window.

      I've tried using scratch buffers with:
      :set buftype=nofile
      :set bufhidden=hide
      :setlocal noswapfile
      But then the name of the buffer is always "[Scratch]", and the buffer is
      lost as soon as the user moves away from it.

      I've also tried using tempname() but then the buffer has a strange name.

      So instead, I've hardcoded the name with:
      let s:diffBufferDiffFile = "/tmp/DiffOutput"
      That gives me the vim behaviour I want but sucks when there are several
      instances of vim.

      Is there a way to use a temporary *file* but name the buffer anyway? Is
      there a pattern / tip I can use for this purpose? I guess I could create
      a dir named tempname() and then create DiffOutput under it...

      Also :help tempname() mentions a 26-times limit. I have the same vim
      running for many days, and only 26 executions of my function is a very
      low limit. I guess I can work around that by calling tempname() once and
      re-use the tempname()/DiffOutput value.

      I'm sure I can experiment and get something working. But there are many
      corner cases to test, and I'm asking for your experience on a pattern
      that works reliably.

      Any takers?

      Peter

      --
      Peter Valdemar Mørch
      http://www.morch.com

      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Ben Schmidt
      ... You can still set the name of a nofile buffer with the :f command. And with bufhidden=hide the buffer is not lost but just hidden when you move away from
      Message 2 of 11 , Sep 8, 2008
        Peter Valdemar Mørch (Lists) wrote:
        > I have a script where I want to show some output to the user. I want the
        > window to have a *meaningful* name ("DiffOutput"), and want the user to
        > be able to switch back and forth between his files and the DiffOutput
        > window.
        >
        > I've tried using scratch buffers with:
        > :set buftype=nofile
        > :set bufhidden=hide
        > :setlocal noswapfile
        > But then the name of the buffer is always "[Scratch]", and the buffer is
        > lost as soon as the user moves away from it.

        You can still set the name of a nofile buffer with the :f command. And
        with bufhidden=hide the buffer is not lost but just hidden when you move
        away from it. You can get it back with the :buf command. It will not be
        saved to disk, and won't stop Vim from exiting, though, even without a
        bang (!) on :q or whatever.

        :setlocal buftype=nofile
        :setlocal bufhidden=hide
        :setlocal noswapfile
        :f DiffOutput
        :e SomethingElse
        :buf DiffOutput

        I can't say I've really had experience doing this, but perhaps it will
        help!

        If you don't want Vim to exit without saving that buffer, you can still
        use :f without setting buftype to nofile; Vim will not exit, nor
        overwrite an existing file with the name you set using :f, without a
        bang.

        Smiles,

        Ben.



        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • Tony Mechelynck
        ... [...] The help seems to say something else: under :help special-buffers , the entry scratch says: scratch Contains text that can be discarded at any
        Message 3 of 11 , Sep 8, 2008
          On 08/09/08 10:05, Peter Valdemar Mørch (Lists) wrote:
          > I have a script where I want to show some output to the user. I want the
          > window to have a *meaningful* name ("DiffOutput"), and want the user to
          > be able to switch back and forth between his files and the DiffOutput
          > window.
          >
          > I've tried using scratch buffers with:
          > :set buftype=nofile
          > :set bufhidden=hide
          > :setlocal noswapfile
          > But then the name of the buffer is always "[Scratch]", and the buffer is
          > lost as soon as the user moves away from it.
          [...]

          The help seems to say something else: under ":help special-buffers", the
          entry "scratch" says:

          scratch Contains text that can be discarded at any time. It is kept
          when closing the window, it must be deleted explicitly.
          Settings: >
          :setlocal buftype=nofile
          :setlocal bufhidden=hide
          :setlocal noswapfile
          < The buffer name can be used to identify the buffer.


          Best regards,
          Tony.
          --
          Brain fried -- Core dumped

          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        • "Peter Valdemar Mørch (Lists)"
          ... FANTASTIC! Exactly what I was looking for. I was quite confused about :help special-buffers saying: The buffer name can be used to identify the buffer.
          Message 4 of 11 , Sep 8, 2008
            Ben Schmidt mail_ben_schmidt-at-yahoo.com.au |Lists| wrote:
            > You can still set the name of a nofile buffer with the :f command.

            FANTASTIC! Exactly what I was looking for.

            I was quite confused about ":help special-buffers" saying: "The buffer
            name can be used to identify the buffer." I was thinking: "How do I set
            that? bufname() is read-only..."

            I tried searching/googling for "buffer name".. At least I didn't end up
            finding ":file" ;-) May I suggest adding that to :help bufname() ?

            Next: After "enew", :file and :ls shows that buffer with the name "[No
            Name]". After fiddling with :setlocal I can get :file and :ls to show
            "[Scratch]".

            Now how do I find the strings "[No Name]" or "[Scratch]" from a script?
            bufname("") returns the empty string, not the strings "[No Name]" or
            "[Scratch]". How can I catch the output from :file into my script?
            Aaarrhh... The joys of learning a new environment...

            Thanks so far!

            Peter

            --
            Peter Valdemar Mørch
            http://www.morch.com

            --~--~---------~--~----~------------~-------~--~----~
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
            -~----------~----~----~----~------~----~------~--~---
          • Tony Mechelynck
            ... If you want a specific name, see :help :file_f . If bufname( ) returns the empty string, then your script can check the other settings: e.g. if &buftype
            Message 5 of 11 , Sep 8, 2008
              On 08/09/08 12:30, Peter Valdemar Mørch (Lists) wrote:
              > Ben Schmidt mail_ben_schmidt-at-yahoo.com.au |Lists| wrote:
              >> You can still set the name of a nofile buffer with the :f command.
              >
              > FANTASTIC! Exactly what I was looking for.
              >
              > I was quite confused about ":help special-buffers" saying: "The buffer
              > name can be used to identify the buffer." I was thinking: "How do I set
              > that? bufname() is read-only..."
              >
              > I tried searching/googling for "buffer name".. At least I didn't end up
              > finding ":file" ;-) May I suggest adding that to :help bufname() ?
              >
              > Next: After "enew", :file and :ls shows that buffer with the name "[No
              > Name]". After fiddling with :setlocal I can get :file and :ls to show
              > "[Scratch]".
              >
              > Now how do I find the strings "[No Name]" or "[Scratch]" from a script?
              > bufname("") returns the empty string, not the strings "[No Name]" or
              > "[Scratch]". How can I catch the output from :file into my script?
              > Aaarrhh... The joys of learning a new environment...
              >
              > Thanks so far!
              >
              > Peter
              >

              If you want a specific name, see ":help :file_f". If bufname('') returns
              the empty string, then your script can check the other settings: e.g. if
              &buftype is empty you've got, IIUC, a [No Name] file.

              Best regards,
              Tony.
              --
              hundred-and-one symptoms of being an internet addict:
              166. You have been on your computer soo long that you didn't realize
              you had grandchildren.

              --~--~---------~--~----~------------~-------~--~----~
              You received this message from the "vim_use" maillist.
              For more information, visit http://www.vim.org/maillist.php
              -~----------~----~----~----~------~----~------~--~---
            • Bob Hiestand
              On Mon, Sep 8, 2008 at 5:30 AM, Peter Valdemar Mørch (Lists) ... You should also be aware of the backtick-equals method of opening ... Why are you trying to
              Message 6 of 11 , Sep 8, 2008
                On Mon, Sep 8, 2008 at 5:30 AM, "Peter Valdemar Mørch (Lists)"
                <4ux6as402@...> wrote:
                >
                > Ben Schmidt mail_ben_schmidt-at-yahoo.com.au |Lists| wrote:
                >> You can still set the name of a nofile buffer with the :f command.

                You should also be aware of the backtick-equals method of opening
                exactly the file name you want:

                :help `=

                > Next: After "enew", :file and :ls shows that buffer with the name "[No
                > Name]". After fiddling with :setlocal I can get :file and :ls to show
                > "[Scratch]".
                >
                > Now how do I find the strings "[No Name]" or "[Scratch]" from a script?
                > bufname("") returns the empty string, not the strings "[No Name]" or
                > "[Scratch]". How can I catch the output from :file into my script?
                > Aaarrhh... The joys of learning a new environment...

                Why are you trying to find buffers without names from a script? There
                may be a better way to do this.

                bob

                --~--~---------~--~----~------------~-------~--~----~
                You received this message from the "vim_use" maillist.
                For more information, visit http://www.vim.org/maillist.php
                -~----------~----~----~----~------~----~------~--~---
              • Ben Schmidt
                ... This is icky. You could special-case it and hard code the strings in, which would break in a localised Vim, though. Or you could capture the output of
                Message 7 of 11 , Sep 8, 2008
                  >> Next: After "enew", :file and :ls shows that buffer with the name "[No
                  >> Name]". After fiddling with :setlocal I can get :file and :ls to show
                  >> "[Scratch]".
                  >>
                  >> Now how do I find the strings "[No Name]" or "[Scratch]" from a script?
                  >> bufname("") returns the empty string, not the strings "[No Name]" or
                  >> "[Scratch]". How can I catch the output from :file into my script?
                  >> Aaarrhh... The joys of learning a new environment...

                  This is icky. You could special-case it and hard code the strings in,
                  which would break in a localised Vim, though. Or you could capture the
                  output of :file! and process it using :redir (e.g. to a variable), which
                  might need careful attention if any files have quotes in their names.

                  > Why are you trying to find buffers without names from a script? There
                  > may be a better way to do this.

                  I think that's probably the case.

                  Ben.



                  --~--~---------~--~----~------------~-------~--~----~
                  You received this message from the "vim_use" maillist.
                  For more information, visit http://www.vim.org/maillist.php
                  -~----------~----~----~----~------~----~------~--~---
                • Peter Valdemar Morch
                  ... Since you seem curious: I m writing a script that will show me the diff between all my buffers and the on-disk files. (E.g. so I know what I still need to
                  Message 8 of 11 , Sep 8, 2008
                    >>> Now how do I find the strings "[No Name]" or "[Scratch]" from a script?
                    >>> bufname("") returns the empty string, not the strings "[No Name]" or
                    >>> "[Scratch]". How can I catch the output from :file into my script?
                    >>> Aaarrhh... The joys of learning a new environment...
                    >
                    > This is icky. You could special-case it and hard code the strings in,
                    > which would break in a localised Vim, though. Or you could capture the
                    > output of :file! and process it using :redir (e.g. to a variable), which
                    > might need careful attention if any files have quotes in their names.
                    >
                    >> Why are you trying to find buffers without names from a script? There
                    >> may be a better way to do this.
                    >
                    > I think that's probably the case.

                    Since you seem curious:

                    I'm writing a script that will show me the diff between all my buffers
                    and the on-disk files. (E.g. so I know what I still need to save with
                    :w). I go through all buffers with :bufdo, and I want to mention the
                    buffers I skip because they don't have associated files (i.e. !
                    strlen(glob(expand("%"))) ).
                    In the output (intended for a human user only), I'd like to show him
                    exactly the strings he sees with :ls or :file.

                    I use git &| svn, and perhaps I just did an svn update or git pull and
                    so the on-disk files are newer. Then I also am creating a func
                    RevertAllBuffers(), that will essentially bufdo("edit!") but skip the
                    exact same non-file buffers. Again, I'd like to show the buffer names
                    exactly as the user sees them and tell that these were skipped.

                    And so I would like to present the [No Name] and [Scratch] strings
                    instead of the current "(Unnamed file)". No biggie. But when I dig into
                    a new environment like I'm doing with vim, I find that it pays off to
                    pay attention to the details and limits of my possibilities.

                    Peter
                    --
                    Peter Valdemar Mørch
                    http://www..........

                    --~--~---------~--~----~------------~-------~--~----~
                    You received this message from the "vim_use" maillist.
                    For more information, visit http://www.vim.org/maillist.php
                    -~----------~----~----~----~------~----~------~--~---
                  • John Beckett
                    ... Make sure you investigate :DiffOrig ( :help :DiffOrig points out it is in an example script). ... Clearly you are just giving a brief (thanks!) summary of
                    Message 9 of 11 , Sep 9, 2008
                      Peter Valdemar Morch wrote:
                      > I'm writing a script that will show me the diff between all
                      > my buffers and the on-disk files.

                      Make sure you investigate :DiffOrig (':help :DiffOrig' points out it is in an
                      example script).

                      > (E.g. so I know what I still need to save with :w).

                      Clearly you are just giving a brief (thanks!) summary of a complex scenario, but
                      since you've said you're new to Vim, note that ':wa' will write all changed buffers
                      (not unchanged), and ':xa' will write all changed buffers and exit.

                      John


                      --~--~---------~--~----~------------~-------~--~----~
                      You received this message from the "vim_use" maillist.
                      For more information, visit http://www.vim.org/maillist.php
                      -~----------~----~----~----~------~----~------~--~---
                    • Peter Valdemar Morch
                      ... Cool. Didn t know about DiffOrig. I much perfer standard diff s output to vim s diff, and I want to see all diffs in my 20 open buffers at once, in a
                      Message 10 of 11 , Sep 9, 2008
                        John Beckett johnb.beckett-at-gmail.com |Lists| wrote:
                        > Make sure you investigate :DiffOrig (':help :DiffOrig' points out it is in an
                        > example script).

                        Cool. Didn't know about DiffOrig. I much perfer standard diff's output
                        to vim's diff, and I want to see all diffs in my 20 open buffers at
                        once, in a single buffer, so I'll stick to my version, but nice to see
                        that someone else needed the functionality!

                        Peter
                        --
                        Peter Valdemar Mørch
                        http://www..........

                        --~--~---------~--~----~------------~-------~--~----~
                        You received this message from the "vim_use" maillist.
                        For more information, visit http://www.vim.org/maillist.php
                        -~----------~----~----~----~------~----~------~--~---
                      • Michael Wookey
                        ... The diffchanges vim script might also be useful - http://www.vim.org/scripts/script.php?script_id=2158
                        Message 11 of 11 , Sep 10, 2008
                          >> Make sure you investigate :DiffOrig (':help :DiffOrig' points out it is in an
                          >> example script).
                          >
                          > Cool. Didn't know about DiffOrig. I much perfer standard diff's output
                          > to vim's diff, and I want to see all diffs in my 20 open buffers at
                          > once, in a single buffer, so I'll stick to my version, but nice to see
                          > that someone else needed the functionality!

                          The diffchanges vim script might also be useful -

                          http://www.vim.org/scripts/script.php?script_id=2158

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