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

Re: how to execute ranger from gvim

Expand Messages
  • ping
    what s the problem with just :!ranger ? ... -- You received this message from the vim_use maillist. Do not top-post! Type your reply below the text you are
    Message 1 of 16 , Dec 3, 2012
    • 0 Attachment
      what's the problem with just :!ranger  ?

      On 08/09/2012 06:02 AM, Sepp Tannhuber wrote:
      I use the following function from the ranger manual:
      fun! RangerChooser()
      exec "silent !ranger --choosefile=/tmp/chosenfile" . expand("%:p:h")
      if filereadable('/tmp/chosenfile')
      exec 'edit ' . system('cat /tmp/chosenfile')
      call system('rm /tmp/chosenfile')
      endif
      redraw!
      endfun
      map ,r :call RangerChooser()<CR>

      --
      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
    • Gary Johnson
      ... The function uses Ranger as a file chooser. Vim will edit the file you ve chosen using Ranger in the current Vim instance. Using just ... Vim instance.
      Message 2 of 16 , Dec 3, 2012
      • 0 Attachment
        On 2012-12-03, ping wrote:

        > On 08/09/2012 06:02 AM, Sepp Tannhuber wrote:
        >>
        >> I use the following function from the ranger manual:
        >> fun! RangerChooser()
        >> exec "silent !ranger --choosefile=/tmp/chosenfile" . expand("%:p:h")
        >> if filereadable('/tmp/chosenfile')
        >> exec 'edit ' . system('cat /tmp/chosenfile')
        >> call system('rm /tmp/chosenfile')
        >> endif
        >> redraw!
        >> endfun
        >> map ,r :call RangerChooser()<CR>

        > what's the problem with just :!ranger ?

        The function uses Ranger as a file chooser. Vim will edit the file
        you've chosen using Ranger in the current Vim instance. Using just
        :!ranger will let you edit a file chosen using Ranger, but in a new
        Vim instance.

        Regards,
        Gary

        --
        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
      • ping
        ... oh I got it now, yes that s great ideal then! but I just did a quick test of your code on vim (not gvim), I move my cursor in ranger to a whatever file
        Message 3 of 16 , Dec 3, 2012
        • 0 Attachment
          On 12/03/2012 12:56 PM, Gary Johnson wrote:
          > On 2012-12-03, ping wrote:
          >
          >> On 08/09/2012 06:02 AM, Sepp Tannhuber wrote:
          >>>
          >>> I use the following function from the ranger manual:
          >>> fun! RangerChooser()
          >>> exec "silent !ranger --choosefile=/tmp/chosenfile" . expand("%:p:h")
          >>> if filereadable('/tmp/chosenfile')
          >>> exec 'edit ' . system('cat /tmp/chosenfile')
          >>> call system('rm /tmp/chosenfile')
          >>> endif
          >>> redraw!
          >>> endfun
          >>> map ,r :call RangerChooser()<CR>
          >
          >> what's the problem with just :!ranger ?
          >
          > The function uses Ranger as a file chooser. Vim will edit the file
          > you've chosen using Ranger in the current Vim instance. Using just
          > :!ranger will let you edit a file chosen using Ranger, but in a new
          > Vim instance.
          >
          > Regards,
          > Gary
          >
          oh I got it now, yes that's great ideal then!

          but I just did a quick test of your code on vim (not gvim), I move my
          cursor in ranger to a whatever file under my home ,say,
          "/home/ping/file1.txt" and hit enter or 'l', I got
          following error:
          [Error 2] No such file or directory: '/tmp/chosenfile/home/ping'
          guess I need to strip off "/tmp/chosenfile"?

          thanks



          --
          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
        • Gary Johnson
          ... It s not my code. The OP claimed he used the function from the ranger manual. I tried that function and got the same error you did. But the function that
          Message 4 of 16 , Dec 3, 2012
          • 0 Attachment
            On 2012-12-03, ping wrote:
            > On 12/03/2012 12:56 PM, Gary Johnson wrote:
            > >On 2012-12-03, ping wrote:
            > >
            > >> On 08/09/2012 06:02 AM, Sepp Tannhuber wrote:
            > >>>
            > >>> I use the following function from the ranger manual:
            > >>> fun! RangerChooser()
            > >>> exec "silent !ranger --choosefile=/tmp/chosenfile" . expand("%:p:h")
            > >>> if filereadable('/tmp/chosenfile')
            > >>> exec 'edit ' . system('cat /tmp/chosenfile')
            > >>> call system('rm /tmp/chosenfile')
            > >>> endif
            > >>> redraw!
            > >>> endfun
            > >>> map ,r :call RangerChooser()<CR>
            > >
            > >> what's the problem with just :!ranger ?
            > >
            > > The function uses Ranger as a file chooser. Vim will edit the file
            > > you've chosen using Ranger in the current Vim instance. Using just
            > > :!ranger will let you edit a file chosen using Ranger, but in a new
            > > Vim instance.
            > >
            > > Regards,
            > > Gary
            > >
            > oh I got it now, yes that's great ideal then!
            >
            > but I just did a quick test of your code on vim (not gvim), I move my
            > cursor in ranger to a whatever file under my home ,say,
            > "/home/ping/file1.txt" and hit enter or 'l', I got
            > following error:
            > [Error 2] No such file or directory: '/tmp/chosenfile/home/ping'
            > guess I need to strip off "/tmp/chosenfile"?

            It's not my code. The OP claimed he used the function from the
            ranger manual.

            I tried that function and got the same error you did. But the
            function that I wrote about a year ago based on one in the ranger
            man page works fine.

            It's not immediately obvious to me what the problem with the OP's
            function is, but here's the one I use.

            ----------------------------- cut here -----------------------------
            " Vim plugin for using ranger as a file chooser
            " File: ranger.vim
            " Maintainer: Gary Johnson <garyjohn AT spocom DOT com>
            " Last Change: 2011-10-31 16:25:58

            " From the ranger(1) man page for ranger-1.5.2:
            "
            " VIM: File Chooser
            " This is a vim function which allows you to use ranger to select a file
            " for opening in your current vim session.
            "
            " fun! RangerChooser()
            " silent !ranger --choosefile=/tmp/chosenfile `[ -z '%' ] && echo -n . || dirname %`
            " if filereadable('/tmp/chosenfile')
            " exec 'edit ' . system('cat /tmp/chosenfile')
            " call system('rm /tmp/chosenfile')
            " endif
            " redraw!
            " endfun
            " map ,r :call RangerChooser()<CR>
            "
            " That function fails when executed in an empty, unnamed buffer with the
            " following messages:
            "
            " Error detected while processing function RangerChooser:
            " line 1:
            " E499: Empty file name for '%' or '#', only works with ":p:h": silent !ranger
            " --choosefile=/tmp/chosenfile `[ -z '%' ] && echo -n . || dirname %`

            " RangerChooser()
            "
            " Ranger version 1.4.2 or later is required to run this function. That's when
            " the --choosefile option was added.

            fun! RangerChooser(...)
            let tmpfile = tempname()
            if a:0 > 0 && a:1 != ""
            let dir = a:1
            elseif expand("%")
            let dir = "."
            else
            let dir = expand("%:p:h")
            endif
            exe 'silent !ranger --choosefile='.tmpfile dir
            if filereadable(tmpfile)
            exe 'edit' readfile(tmpfile)[0]
            call delete(tmpfile)
            endif
            redraw!
            endfun
            "map ,r :call RangerChooser()<CR>
            command -nargs=? RangerChooser call RangerChooser("<args>")
            --------------------------------------------------------------------

            Regards,
            Gary

            --
            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
          • ping
            ... . || dirname %` ... silent !ranger ... That s when ... that are really magic codes and great vim+ranger combination. It works, I don t quite understand
            Message 5 of 16 , Dec 3, 2012
            • 0 Attachment

              On 12/03/2012 01:44 PM, Gary Johnson wrote:
              > On 2012-12-03, ping wrote:
              >> On 12/03/2012 12:56 PM, Gary Johnson wrote:
              >>> On 2012-12-03, ping wrote:
              >>>
              >>>> On 08/09/2012 06:02 AM, Sepp Tannhuber wrote:
              >>>>>
              >>>>> I use the following function from the ranger manual:
              >>>>> fun! RangerChooser()
              >>>>> exec "silent !ranger --choosefile=/tmp/chosenfile" . expand("%:p:h")
              >>>>> if filereadable('/tmp/chosenfile')
              >>>>> exec 'edit ' . system('cat /tmp/chosenfile')
              >>>>> call system('rm /tmp/chosenfile')
              >>>>> endif
              >>>>> redraw!
              >>>>> endfun
              >>>>> map ,r :call RangerChooser()<CR>
              >>>
              >>>> what's the problem with just :!ranger ?
              >>>
              >>> The function uses Ranger as a file chooser. Vim will edit the file
              >>> you've chosen using Ranger in the current Vim instance. Using just
              >>> :!ranger will let you edit a file chosen using Ranger, but in a new
              >>> Vim instance.
              >>>
              >>> Regards,
              >>> Gary
              >>>
              >> oh I got it now, yes that's great ideal then!
              >>
              >> but I just did a quick test of your code on vim (not gvim), I move my
              >> cursor in ranger to a whatever file under my home ,say,
              >> "/home/ping/file1.txt" and hit enter or 'l', I got
              >> following error:
              >> [Error 2] No such file or directory: '/tmp/chosenfile/home/ping'
              >> guess I need to strip off "/tmp/chosenfile"?
              >
              > It's not my code. The OP claimed he used the function from the
              > ranger manual.
              >
              > I tried that function and got the same error you did. But the
              > function that I wrote about a year ago based on one in the ranger
              > man page works fine.
              >
              > It's not immediately obvious to me what the problem with the OP's
              > function is, but here's the one I use.
              >
              > ----------------------------- cut here -----------------------------
              > " Vim plugin for using ranger as a file chooser
              > " File: ranger.vim
              > " Maintainer: Gary Johnson <garyjohn AT spocom DOT com>
              > " Last Change: 2011-10-31 16:25:58
              >
              > " From the ranger(1) man page for ranger-1.5.2:
              > "
              > " VIM: File Chooser
              > " This is a vim function which allows you to use ranger to select a file
              > " for opening in your current vim session.
              > "
              > " fun! RangerChooser()
              > " silent !ranger --choosefile=/tmp/chosenfile `[ -z '%' ] && echo -n . || dirname %`
              > " if filereadable('/tmp/chosenfile')
              > " exec 'edit ' . system('cat /tmp/chosenfile')
              > " call system('rm /tmp/chosenfile')
              > " endif
              > " redraw!
              > " endfun
              > " map ,r :call RangerChooser()<CR>
              > "
              > " That function fails when executed in an empty, unnamed buffer with the
              > " following messages:
              > "
              > " Error detected while processing function RangerChooser:
              > " line 1:
              > " E499: Empty file name for '%' or '#', only works with ":p:h": silent !ranger
              > " --choosefile=/tmp/chosenfile `[ -z '%' ] && echo -n . || dirname %`
              >
              > " RangerChooser()
              > "
              > " Ranger version 1.4.2 or later is required to run this function. That's when
              > " the --choosefile option was added.
              >
              > fun! RangerChooser(...)
              > let tmpfile = tempname()
              > if a:0 > 0 && a:1 != ""
              > let dir = a:1
              > elseif expand("%")
              > let dir = "."
              > else
              > let dir = expand("%:p:h")
              > endif
              > exe 'silent !ranger --choosefile='.tmpfile dir
              > if filereadable(tmpfile)
              > exe 'edit' readfile(tmpfile)[0]
              > call delete(tmpfile)
              > endif
              > redraw!
              > endfun
              > "map ,r :call RangerChooser()<CR>
              > command -nargs=? RangerChooser call RangerChooser("<args>")
              > --------------------------------------------------------------------
              >
              > Regards,
              > Gary
              >


              that are really magic codes and great vim+ranger combination. It works, I don't quite understand exactly how it works though...

              regards
              ping

              --
              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
            • Gary Johnson
              ... Setting dir that way doesn t make sense to me. Either I wasn t thinking straight when I wrote it or I m not thinking straight now. ... I annotated the
              Message 6 of 16 , Dec 3, 2012
              • 0 Attachment
                On 2012-12-03, ping wrote:
                >
                > On 12/03/2012 01:44 PM, Gary Johnson wrote:

                > > " RangerChooser()
                > > "
                > > " Ranger version 1.4.2 or later is required to run this function. That's when
                > > " the --choosefile option was added.
                > >
                > > fun! RangerChooser(...)
                > > let tmpfile = tempname() " Get the name of a temporary
                > > " file.
                > > if a:0 > 0 && a:1 != "" " If the user has supplied an
                > > " argument,
                > > let dir = a:1 " set dir to that argument,
                > > elseif expand("%") " otherwise if the current
                > > " buffer has a name,
                > > let dir = "." " set dir to the current
                > > " working directory.
                > > else " Otherwise,
                > > let dir = expand("%:p:h") " set dir to the directory
                > > " containing the current
                > > " buffer.

                Setting dir that way doesn't make sense to me. Either I wasn't
                thinking straight when I wrote it or I'm not thinking straight now.

                > > endif
                > > exe 'silent !ranger --choosefile='.tmpfile dir
                > > " Execute ranger in the
                > > " directory given by dir and
                > > " put the selected file in
                > > " tmpfile.
                > > if filereadable(tmpfile) " If tmpfile is readable,
                > > exe 'edit' readfile(tmpfile)[0]
                > > " edit the file whose name
                > > " is in the first line of
                > > " tmpfile. (readfile()
                > > " returns the contents of the
                > > " file as a list of lines.
                > > " List element 0 is the
                > > " first line.
                > > call delete(tmpfile) " Delete the temporary file.
                > > endif
                > > redraw! " The display may have been
                > > " messed up by ranger, so
                > > " redraw it.
                > > endfun
                > > "map ,r :call RangerChooser()<CR>
                > > command -nargs=? RangerChooser call RangerChooser("<args>")

                > that are really magic codes and great vim+ranger combination. It works, I don't
                > quite understand exactly how it works though...

                I annotated the function above.

                HTH,
                Gary

                --
                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
              • Gary Johnson
                ... I think I must have intended that to be the following, which is the same as above but with the last two dependent clauses interchanged. if a:0 0 && a:1
                Message 7 of 16 , Dec 3, 2012
                • 0 Attachment
                  On 2012-12-03, Gary Johnson wrote:
                  > On 2012-12-03, ping wrote:
                  > >
                  > > On 12/03/2012 01:44 PM, Gary Johnson wrote:
                  >
                  > > > " RangerChooser()
                  > > > "
                  > > > " Ranger version 1.4.2 or later is required to run this function. That's when
                  > > > " the --choosefile option was added.
                  > > >
                  > > > fun! RangerChooser(...)
                  > > > let tmpfile = tempname() " Get the name of a temporary
                  > > > " file.
                  > > > if a:0 > 0 && a:1 != "" " If the user has supplied an
                  > > > " argument,
                  > > > let dir = a:1 " set dir to that argument,
                  > > > elseif expand("%") " otherwise if the current
                  > > > " buffer has a name,
                  > > > let dir = "." " set dir to the current
                  > > > " working directory.
                  > > > else " Otherwise,
                  > > > let dir = expand("%:p:h") " set dir to the directory
                  > > > " containing the current
                  > > > " buffer.
                  >
                  > Setting dir that way doesn't make sense to me. Either I wasn't
                  > thinking straight when I wrote it or I'm not thinking straight now.

                  I think I must have intended that to be the following, which is the
                  same as above but with the last two dependent clauses interchanged.

                  if a:0 > 0 && a:1 != "" " If the user has supplied an
                  " argument,
                  let dir = a:1 " set dir to that argument,
                  elseif expand("%") " otherwise if the current
                  " buffer has a name,
                  let dir = expand("%:p:h") " set dir to the directory
                  " containing the current
                  " buffer.
                  else " Otherwise,
                  let dir = "." " set dir to the current
                  " working directory.

                  Regards,
                  Gary

                  --
                  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
                • ping
                  ... thanks for the line by line annotation, now I understand! just one last small thing, in the above line, won t it suffice just: exe readfile(tmpfile) ? my
                  Message 8 of 16 , Dec 3, 2012
                  • 0 Attachment
                    On 12/3/2012 8:06 PM, Gary Johnson wrote:
                    > exe 'edit' readfile(tmpfile)[0]
                    > > > " edit the file whose name
                    > > > " is in the first line of
                    > > > " tmpfile. (readfile()
                    > > > " returns the contents of the
                    > > > " file as a list of lines.
                    > > > " List element 0 is the
                    > > > " first line.
                    thanks for the line by line annotation, now I understand!

                    just one last small thing, in the above line, won't it suffice just:
                    exe readfile(tmpfile)
                    ?

                    my test shows that will only give me a line of the file name, but now
                    the file contents so you are right,
                    but help says:
                    readfile({fname} [, {binary} [, {max}]])
                    Read file {fname} and return a List, each line of the file
                    as an item. Lines broken at NL characters. Macintosh files
                    separated with CR will result in a single long line
                    (unless a
                    NL appears somewhere).

                    so readfile should have "read" the "file", why I only get the filename
                    instead?



                    thanks.

                    regards
                    ping

                    --
                    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
                  • Gary Johnson
                    ... I m not sure what you are expecting that command to do. The :exe command executes its argument string, so its argument string must be an executable Vim
                    Message 9 of 16 , Dec 3, 2012
                    • 0 Attachment
                      On 2012-12-03, ping wrote:
                      > On 12/3/2012 8:06 PM, Gary Johnson wrote:
                      > > exe 'edit' readfile(tmpfile)[0]
                      > >> > " edit the file whose name
                      > >> > " is in the first line of
                      > >> > " tmpfile. (readfile()
                      > >> > " returns the contents of the
                      > >> > " file as a list of lines.
                      > >> > " List element 0 is the
                      > >> > " first line.
                      > thanks for the line by line annotation, now I understand!
                      >
                      > just one last small thing, in the above line, won't it suffice just:
                      > exe readfile(tmpfile)
                      > ?

                      I'm not sure what you are expecting that command to do. The :exe
                      command executes its argument string, so its argument string must be
                      an executable Vim command. The readline() function returns a list
                      (which is not a string), and the first element of that list will in
                      this case be a file name. A file name is not an executable Vim
                      command.

                      > my test shows that will only give me a line of the file name, but now
                      > the file contents so you are right,
                      > but help says:
                      > readfile({fname} [, {binary} [, {max}]])
                      > Read file {fname} and return a List, each line of the file
                      > as an item. Lines broken at NL characters. Macintosh files
                      > separated with CR will result in a single long line
                      > (unless a
                      > NL appears somewhere).
                      >
                      > so readfile should have "read" the "file", why I only get the
                      > filename instead?

                      The readfile() function read the file whose name was the value of
                      the tmpfile variable. That file contained the name of the file you
                      selected in ranger.

                      Maybe an example would be clearer.

                      Let's say you execute :RangerChooser in an unnamed buffer. The
                      tempname() function returns the name of a temporary file, something
                      like /tmp/vuZJYgI/2, so ranger is executed with arguments like this:

                      ranger --choosefile=/tmp/vuZJYgI/2 .

                      You browse your home directory and select a file named hello.txt.
                      Ranger saves the string "/home/ping/hello.txt" into the file
                      /tmp/vuZJYgI/2 and exits. Vim now executes
                      readfile("/tmp/vuZJYgI/2") which returns the list

                      ['/home/ping/hello.txt']

                      The zeroth element of that list, specified in the function as

                      readfile(tmpfile)[0]

                      is the string '/home/ping/hello.txt'. The command

                      exe 'edit' readfile(tmpfile)[0]

                      evaluates to

                      edit '/home/ping/hello.txt'

                      which is the desired result.

                      Regards,
                      Gary

                      --
                      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
                    • ping
                      ... that is super clear !(I also got it this morning on my way driving to work :D ) good learning for me, thanks for all the explanations. -- You received this
                      Message 10 of 16 , Dec 4, 2012
                      • 0 Attachment
                        On 12/04/2012 01:19 AM, Gary Johnson wrote:
                        > On 2012-12-03, ping wrote:
                        >> On 12/3/2012 8:06 PM, Gary Johnson wrote:
                        >>> exe 'edit' readfile(tmpfile)[0]
                        >>>>> " edit the file whose name
                        >>>>> " is in the first line of
                        >>>>> " tmpfile. (readfile()
                        >>>>> " returns the contents of the
                        >>>>> " file as a list of lines.
                        >>>>> " List element 0 is the
                        >>>>> " first line.
                        >> thanks for the line by line annotation, now I understand!
                        >>
                        >> just one last small thing, in the above line, won't it suffice just:
                        >> exe readfile(tmpfile)
                        >> ?
                        > I'm not sure what you are expecting that command to do. The :exe
                        > command executes its argument string, so its argument string must be
                        > an executable Vim command. The readline() function returns a list
                        > (which is not a string), and the first element of that list will in
                        > this case be a file name. A file name is not an executable Vim
                        > command.
                        >
                        >> my test shows that will only give me a line of the file name, but now
                        >> the file contents so you are right,
                        >> but help says:
                        >> readfile({fname} [, {binary} [, {max}]])
                        >> Read file {fname} and return a List, each line of the file
                        >> as an item. Lines broken at NL characters. Macintosh files
                        >> separated with CR will result in a single long line
                        >> (unless a
                        >> NL appears somewhere).
                        >>
                        >> so readfile should have "read" the "file", why I only get the
                        >> filename instead?
                        > The readfile() function read the file whose name was the value of
                        > the tmpfile variable. That file contained the name of the file you
                        > selected in ranger.
                        >
                        > Maybe an example would be clearer.
                        >
                        > Let's say you execute :RangerChooser in an unnamed buffer. The
                        > tempname() function returns the name of a temporary file, something
                        > like /tmp/vuZJYgI/2, so ranger is executed with arguments like this:
                        >
                        > ranger --choosefile=/tmp/vuZJYgI/2 .
                        >
                        > You browse your home directory and select a file named hello.txt.
                        > Ranger saves the string "/home/ping/hello.txt" into the file
                        > /tmp/vuZJYgI/2 and exits. Vim now executes
                        > readfile("/tmp/vuZJYgI/2") which returns the list
                        >
                        > ['/home/ping/hello.txt']
                        >
                        > The zeroth element of that list, specified in the function as
                        >
                        > readfile(tmpfile)[0]
                        >
                        > is the string '/home/ping/hello.txt'. The command
                        >
                        > exe 'edit' readfile(tmpfile)[0]
                        >
                        > evaluates to
                        >
                        > edit '/home/ping/hello.txt'
                        >
                        > which is the desired result.
                        >
                        > Regards,
                        > Gary
                        that is super clear !(I also got it this morning on my way driving to
                        work :D )
                        good learning for me, thanks for all the explanations.

                        --
                        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
                      • ping
                        ... a little bit enhance -- to make ranger continue with where it left off. I think there should be also a way to make the cursor stay on the last exited
                        Message 11 of 16 , Dec 5, 2012
                        • 0 Attachment

                          On 12/04/2012 01:19 AM, Gary Johnson wrote:
                          > On 2012-12-03, ping wrote:
                          >> On 12/3/2012 8:06 PM, Gary Johnson wrote:
                          >>> exe 'edit' readfile(tmpfile)[0]
                          >>>>> " edit the file whose name
                          >>>>> " is in the first line of
                          >>>>> " tmpfile. (readfile()
                          >>>>> " returns the contents of the
                          >>>>> " file as a list of lines.
                          >>>>> " List element 0 is the
                          >>>>> " first line.
                          >> thanks for the line by line annotation, now I understand!
                          >>
                          >> just one last small thing, in the above line, won't it suffice just:
                          >> exe readfile(tmpfile)
                          >> ?
                          >
                          > I'm not sure what you are expecting that command to do. The :exe
                          > command executes its argument string, so its argument string must be
                          > an executable Vim command. The readline() function returns a list
                          > (which is not a string), and the first element of that list will in
                          > this case be a file name. A file name is not an executable Vim
                          > command.
                          >
                          >> my test shows that will only give me a line of the file name, but now
                          >> the file contents so you are right,
                          >> but help says:
                          >> readfile({fname} [, {binary} [, {max}]])
                          >> Read file {fname} and return a List, each line of the file
                          >> as an item. Lines broken at NL characters. Macintosh files
                          >> separated with CR will result in a single long line
                          >> (unless a
                          >> NL appears somewhere).
                          >>
                          >> so readfile should have "read" the "file", why I only get the
                          >> filename instead?
                          >
                          > The readfile() function read the file whose name was the value of
                          > the tmpfile variable. That file contained the name of the file you
                          > selected in ranger.
                          >
                          > Maybe an example would be clearer.
                          >
                          > Let's say you execute :RangerChooser in an unnamed buffer. The
                          > tempname() function returns the name of a temporary file, something
                          > like /tmp/vuZJYgI/2, so ranger is executed with arguments like this:
                          >
                          > ranger --choosefile=/tmp/vuZJYgI/2 .
                          >
                          > You browse your home directory and select a file named hello.txt.
                          > Ranger saves the string "/home/ping/hello.txt" into the file
                          > /tmp/vuZJYgI/2 and exits. Vim now executes
                          > readfile("/tmp/vuZJYgI/2") which returns the list
                          >
                          > ['/home/ping/hello.txt']
                          >
                          > The zeroth element of that list, specified in the function as
                          >
                          > readfile(tmpfile)[0]
                          >
                          > is the string '/home/ping/hello.txt'. The command
                          >
                          > exe 'edit' readfile(tmpfile)[0]
                          >
                          > evaluates to
                          >
                          > edit '/home/ping/hello.txt'
                          >
                          > which is the desired result.
                          >
                          > Regards,
                          > Gary
                          >


                          a little bit enhance -- to make ranger continue with where it left off.


                          I think there should be also a way to make the cursor stay on the last
                          exited file(but not open) when re-run?

                          if !exists("Rangerdir")
                              let RangerDir='.'
                          endif

                          fun! RangerChooser(...)
                           
                          "define a temp var to store name of a temp file, which was use
                          "by ranger to store name of the file to be opened
                          "
                              let tmpfile = tempname()     "generate a temp name,like '/tmp/vuZJYgI/2'
                              let tmpdir = tempname()     "for file and dir, for ranger use

                          "determine what dir that ranger will start with
                              if g:RangerDir != '.'     "if dir get changed (by ranger)
                              let dir = g:RangerDir     "continue from the last changed dir
                              elseif a:0 > 0 && a:1 != ""    "if not changed,if supplied an non-empty
                              let dir = a:1         "argument from user,use it
                              elseif expand("%")         "if current buffer has a name
                              let dir = expand("%:p:h") "use dir containing curr buffer
                              else             "otherwise(there is no even curr named buffer)
                              let dir = '.'         "start from current working dir
                              endif

                          "call ranger: ranger --choosefile=tmpfile $dir
                          "start from 'dir', when exit,
                          "save selected filename/foldername to temp file named 'tmpfile'/'tmpdir'
                              exe 'silent !ranger --choosefile='.tmpfile '--choosedir='.tmpdir dir
                              "read and save the real folder name from the saved temp file
                              if filereadable(tmpdir)     "if tempfile is readable
                              "read tmpfile for the filename, then edit in curr buff
                              let g:RangerDir=readfile(tmpdir)[0]
                              call delete(tmpdir)     "delete the tmpfile
                              endif

                              "retrieve and save the real file name out of the saved temp file
                              if filereadable(tmpfile)     "if tempfile is readable
                              "read tmpfile for the filename, then edit in curr buff
                              exe 'edit' readfile(tmpfile)[0]
                              call delete(tmpfile)     "delete the tmpfile
                              endif
                              redraw!
                          endfun
                          map ,r :call RangerChooser()<CR>
                          command -nargs=? RangerChooser call RangerChooser("<args>")

                          B.t.w, per man ranger there is also a bashrc way to do it.

                          Bash: cd to last path after exit
                              This is a bash function (for ~/.bashrc) to change the directory to
                              the last visited one after ranger quits.  You can always type "cd -"
                              to go back to the original one.

                               function ranger-cd {
                                 tempfile='/tmp/chosendir'
                                 /usr/bin/ranger --choosedir="$tempfile" "${@:-$(pwd)}"
                                 test -f "$tempfile" &&
                                 if [ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]; then
                                   cd -- "$(cat "$tempfile")"
                                 fi
                                 rm -f -- "$tempfile"
                               }

                               # This binds Ctrl-O to ranger-cd:
                               bind '"\C-o":"ranger-cd\C-m"'


                          --
                          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
                        Your message has been successfully submitted and would be delivered to recipients shortly.