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

Checking in vim script if the buffer is new

Expand Messages
  • Pavel Shevaev
    Folks, what is the best way to check in some function if the buffer is new and is not attached to any file yet? Thanks in advance. -- Best regards, Pavel
    Message 1 of 5 , Oct 29, 2007
    • 0 Attachment
      Folks, what is the best way to check in some function if the buffer is
      new and is not attached to any file yet?

      Thanks in advance.

      --
      Best regards, Pavel

      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Tony Mechelynck
      ... - To check for a [No Name] buffer: if bufname( % ) == - To check if the buffer is empty (i.e., contains no data): if line( $ ) == 1 && getline(1) ==
      Message 2 of 5 , Oct 29, 2007
      • 0 Attachment
        Pavel Shevaev wrote:
        > Folks, what is the best way to check in some function if the buffer is
        > new and is not attached to any file yet?
        >
        > Thanks in advance.
        >

        - To check for a [No Name] buffer:

        if bufname('%') == ''

        - To check if the buffer is empty (i.e., contains no data):

        if line('$') == 1 && getline(1) == ''

        (note: "if line2byte(line('$') + 1) == -1" will give TRUE for an empty file
        but also for any file if +byte_offset hasn't been compiled-in.)


        Best regards,
        Tony.
        --
        hundred-and-one symptoms of being an internet addict:
        97. Your mother tells you to remember something, and you look for
        a File/Save command.

        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • Pavel Shevaev
        ... Thank you very much, I used this for the following stuff: function! UpdateIfNotNew() if bufname( % ) != update endif endfunction Hitting will
        Message 3 of 5 , Oct 30, 2007
        • 0 Attachment
          On 10/30/07, Tony Mechelynck <antoine.mechelynck@...> wrote:

          > - To check for a [No Name] buffer:
          >
          > if bufname('%') == ''
          >

          Thank you very much, I used this for the following stuff:

          function! UpdateIfNotNew()
          if bufname('%') != ''
          update
          endif
          endfunction
          " Hitting <Esc> will automatically update the buffer
          map <silent> <Esc> <Esc>:call UpdateIfNotNew()<CR>
          imap <silent> <Esc> <Esc>:call UpdateIfNotNew()<CR>

          I often find myself typing :w each time I hit <Esc> that's why I
          decided to automate this procedure a little bit ;)

          --
          Best regards, Pavel

          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        • Tony Mechelynck
          ... Nice idea. I shall change my F3 mapping from ... to ... (N.B. I don t use hidden buffers.) Best regards, Tony. -- Atlanta makes it against the law to tie
          Message 4 of 5 , Oct 30, 2007
          • 0 Attachment
            Pavel Shevaev wrote:
            > On 10/30/07, Tony Mechelynck <antoine.mechelynck@...> wrote:
            >
            >> - To check for a [No Name] buffer:
            >>
            >> if bufname('%') == ''
            >>
            >
            > Thank you very much, I used this for the following stuff:
            >
            > function! UpdateIfNotNew()
            > if bufname('%') != ''
            > update
            > endif
            > endfunction
            > " Hitting <Esc> will automatically update the buffer
            > map <silent> <Esc> <Esc>:call UpdateIfNotNew()<CR>
            > imap <silent> <Esc> <Esc>:call UpdateIfNotNew()<CR>
            >
            > I often find myself typing :w each time I hit <Esc> that's why I
            > decided to automate this procedure a little bit ;)
            >

            Nice idea. I shall change my F3 mapping

            from
            :map <F3> :wa<Bar>wv<CR>
            to
            :map <F3> :wv<Bar>windo if bufname('%') != ''<Bar>update<Bar>endif<CR>

            (N.B. I don't use 'hidden' buffers.)


            Best regards,
            Tony.
            --
            Atlanta makes it against the law to tie a giraffe to a telephone pole
            or street lamp.

            --~--~---------~--~----~------------~-------~--~----~
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
            -~----------~----~----~----~------~----~------~--~---
          • Hari Krishna Dara
            ... Notice that this approach will fail if you open a buffer with a name, but it hasn t yet been written. It is still a new file (by your definition of not
            Message 5 of 5 , Oct 30, 2007
            • 0 Attachment
              On Tue, 30 Oct 2007 at 10:33am, Pavel Shevaev wrote:

              >
              > On 10/30/07, Tony Mechelynck <antoine.mechelynck@...> wrote:
              >
              >> - To check for a [No Name] buffer:
              >>
              >> if bufname('%') == ''
              >>
              >
              > Thank you very much, I used this for the following stuff:
              >
              > function! UpdateIfNotNew()
              > if bufname('%') != ''
              > update
              > endif
              > endfunction

              Notice that this approach will fail if you open a buffer with a name,
              but it hasn't yet been written. It is still a new file (by your
              definition of not associating with a file yet). Not sure if this
              information is readily avaialable, but it is possible to check
              the output of :file for the token "[New]" (language sensitive) or use
              more elaborate approach of turning on a flag with BufNew and clearing it
              up with BufWrite. I would also recommend to make sure 'buftype' is not
              set before calling 'update'.

              --
              HTH,
              Hari

              > " Hitting <Esc> will automatically update the buffer
              > map <silent> <Esc> <Esc>:call UpdateIfNotNew()<CR>
              > imap <silent> <Esc> <Esc>:call UpdateIfNotNew()<CR>
              >
              > I often find myself typing :w each time I hit <Esc> that's why I
              > decided to automate this procedure a little bit ;)
              >
              >

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