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

Re: expand('%:p') doesn't work properly when opening a new file in a non-existing directory?

Expand Messages
  • Tony Mechelynck
    ... In this case, trying to write the file will answer a/a.c: E212 Cannot open file for writing , showing that there is *no* path to the file. But after
    Message 1 of 4 , Mar 29, 2013
    • 0 Attachment
      On 30/03/13 03:50, hong@... wrote:
      > Hi All,
      >
      > According to the document of expand, "expand('%:p')" should be able to give full path of current buffer. This works properly when use vim to open an existing file or create a new file in an existing directory. But If I execute this command:
      >
      > gvim a/a.c
      >
      > where a is a non-existing directory. Then I run ":echo expand('%:p')", it gives 'a/a.c', which is not what I expected. Other modifiers just work as expected except '%:r'.
      >
      > Anyone have any ideas?
      >
      > Thanks!
      > Hong
      >

      In this case, trying to write the file will answer "a/a.c: E212 Cannot
      open file for writing", showing that there is *no* path to the file. But
      after creating a subdirectory ./a in the current directory,
      expand('%:p') gets a sensible value, and the write (if attempted) succeeds.

      On Unix you might try
      :exe '!mkdir -pv' expand('%:h')
      :w
      if you really want to save the file under that name. (Of course it will
      fail if you don't have the necessary directory-creation permissions.)


      Best regards,
      Tony.
      --
      Machine-Independent, adj.:
      Does not run on any existing machine.

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

      ---
      You received this message because you are subscribed to the Google Groups "vim_use" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • Hong Xu
      ... Hi Tony, Thanks for your reply. I have noticed that I can not write to disk if I create a new file in a non-existing directory; what I am concerning is the
      Message 2 of 4 , Mar 29, 2013
      • 0 Attachment
        On Friday, March 29, 2013 9:14:17 PM UTC-7, Tony Mechelynck wrote:

        >> On 30/03/13 03:50, hong@... wrote:
        >>
        >>> Hi All,
        >>
        >>>
        >>
        >>> According to the document of expand, "expand('%:p')" should be able to give full path of current buffer. This works properly when use vim to open an existing file or create a new file in an existing directory. But If I execute this command:
        >>
        >>>
        >>
        >>> gvim a/a.c
        >>
        >>>
        >>
        >>> where a is a non-existing directory. Then I run ":echo expand('%:p')", it gives 'a/a.c', which is not what I expected. Other modifiers just work as expected except '%:r'.
        >>
        >>>
        >>
        >>> Anyone have any ideas?
        >>
        >>>
        >>
        >>> Thanks!
        >>
        >>> Hong
        >>
        >>>
        >>
        >>
        >>
        >> In this case, trying to write the file will answer "a/a.c: E212 Cannot
        >>
        >> open file for writing", showing that there is *no* path to the file. But
        >>
        >> after creating a subdirectory ./a in the current directory,
        >>
        >> expand('%:p') gets a sensible value, and the write (if attempted) succeeds.
        >>
        >>
        >>
        >> On Unix you might try
        >>
        >> :exe '!mkdir -pv' expand('%:h')
        >>
        >> :w
        >>
        >> if you really want to save the file under that name. (Of course it will
        >>
        >> fail if you don't have the necessary directory-creation permissions.)
        >>
        >>

        Hi Tony,

        Thanks for your reply.

        I have noticed that I can not write to disk if I create a new file in a
        non-existing directory; what I am concerning is the unexpected behavior
        of `expand('%:p')`. I am currently encountering an issue in my plugin: I
        call `expand('%:p')` to obtain the full path, but in the given case
        above, I would get a truncated path. So I think this behavior should not
        be expected. Do you think so?

        Thanks,
        Hong

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

        ---
        You received this message because you are subscribed to the Google Groups "vim_use" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • Tony Mechelynck
        On 30/03/13 07:35, Hong Xu wrote: [...] ... I think that you cannot get the full path if there is no disk path. Maybe you ought to get rather than a/a.b .
        Message 3 of 4 , Mar 30, 2013
        • 0 Attachment
          On 30/03/13 07:35, Hong Xu wrote:
          [...]
          > Hi Tony,
          >
          > Thanks for your reply.
          >
          > I have noticed that I can not write to disk if I create a new file in a
          > non-existing directory; what I am concerning is the unexpected behavior
          > of `expand('%:p')`. I am currently encountering an issue in my plugin: I
          > call `expand('%:p')` to obtain the full path, but in the given case
          > above, I would get a truncated path. So I think this behavior should not
          > be expected. Do you think so?
          >
          > Thanks,
          > Hong
          >

          I think that you cannot get the full path if there is no disk path.
          Maybe you ought to get '' rather than 'a/a.b'. Or maybe it is intended
          (perhaps vi-compatible) behaviour, and you can test the value (on Unix:
          either empty, or doesn't start with a slash; on Windows: either empty,
          or the second character is not a colon) to determine that you didn't get
          a real path. (Empty is for a [No Name] buffer.)

          " untested
          let filepath = expand('%:p')
          if ((has('unix') && (filepath =~ '/.*')) || (!has('unix') && (filepath
          =~ '.:.*'))
          " we have a real path
          else
          " we don't
          endif

          Now that I think of it, a better test might be filereadable() or
          filewritable(). Or :w in a try-catch block.

          See
          :help filereadable()
          :help filewritable()
          :help :try

          Best regards,
          Tony.
          --
          INSPECTOR END OF FILM: Move along. There's nothing to see! Keep moving!
          [Suddenly he notices the cameras.]
          INSPECTOR END OF FILM: (to Camera) All right, put that away sonny.
          [He walks over to it and puts his hand over the lens.]
          "Monty Python and the Holy Grail" PYTHON (MONTY)
          PICTURES LTD

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

          ---
          You received this message because you are subscribed to the Google Groups "vim_use" group.
          To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
          For more options, visit https://groups.google.com/groups/opt_out.
        Your message has been successfully submitted and would be delivered to recipients shortly.