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

Function To Search Directory And Display File List

Expand Messages
  • Tom Purl
    Hi, I m a Vim scripting newbie, and had a pretty general question. I m would like to write a function that does the following: 1. Searches a directory for all
    Message 1 of 7 , Jul 13, 2006
    • 0 Attachment
      Hi, I'm a Vim scripting newbie, and had a pretty general question.

      I'm would like to write a function that does the following:

      1. Searches a directory for all files that end in the string "Template"
      2. Return those file names to the user as chooseable list, similar to
      the way that the spell checker functionality works when you use the z=
      command.

      Would anyone know how to do this? I checked the functions help in Vim,
      but couldn't find what I was looking for.

      Thanks in advance for any guidance!

      Tom Purl
    • Hari Krishna Dara
      ... From your description, it is not clear if this will be part of a larger script that you are developing. If all that you want to do is ability to search and
      Message 2 of 7 , Jul 13, 2006
      • 0 Attachment
        On Thu, 13 Jul 2006 at 12:58pm, Tom Purl wrote:

        > Hi, I'm a Vim scripting newbie, and had a pretty general question.
        >
        > I'm would like to write a function that does the following:
        >
        > 1. Searches a directory for all files that end in the string "Template"
        > 2. Return those file names to the user as chooseable list, similar to
        > the way that the spell checker functionality works when you use the z=
        > command.
        >
        > Would anyone know how to do this? I checked the functions help in Vim,
        > but couldn't find what I was looking for.
        >
        > Thanks in advance for any guidance!
        >
        > Tom Purl
        >

        From your description, it is not clear if this will be part of a larger
        script that you are developing. If all that you want to do is ability to
        search and open the file, one of the existing plugins might be able to
        help you. But if you want to ask the user to select one and do something
        with that selection, you can use the glob() and inputlist() functions to
        do this, something like this:

        let files = glob(dir.'*Template')
        if files != ''
        let filelist = split(files, "\n")
        let sel = inputlist(['Select file:']+filelist)
        if sel != -1
        let selectedFile = filelist[sel]
        endif
        endif

        --
        HTH,
        Hari

        __________________________________________________
        Do You Yahoo!?
        Tired of spam? Yahoo! Mail has the best spam protection around
        http://mail.yahoo.com
      • Tim Chase
        ... ^ %([^[:cntrl:]]*[[:cntrl:]] ) { .(confirm( Which file? , s)). } ([^[:cntrl:]]* ).* , 1 , g ) Those two lines create a prompt with available matches
        Message 3 of 7 , Jul 13, 2006
        • 0 Attachment
          > 1. Searches a directory for all files that end in the string "Template"
          > 2. Return those file names to the user as chooseable list, similar to
          > the way that the spell checker functionality works when you use the z=
          > command.


          Well, as an ugly first-pass hack:

          :let s = expand("*template")
          :echo substitute("\n".s."\n",
          '^\%([^[:cntrl:]]*[[:cntrl:]]\)\{'.(confirm('Which file?',
          s)).'}\([^[:cntrl:]]*\).*', '\1', 'g')

          Those two lines create a prompt with available matches for
          whatever is stashed in "s".

          Instead of an :echo you can do an assignment, or make it the
          return value of a function so you don't have to recreate this
          beast repeatedly.

          It can be wrapped in a function if desired. Hari's suggestion
          was a good one, but requires Vim7 (at least it didn't work on 6.3
          when I tried it because of the need for the inputlist() and
          split() functions). As you reference the "like spell-checker",
          you're likely running 7.0 and thus are safe with his much clearer
          method.

          The above two-liner, despite its brutish opacity, should work at
          least back to 6.0.

          -tim
        • Tom Purl
          ... I m adding a function to the potwiki script so that I can create a new wiki page based on a template. ... This example worked very well for me. Thanks a
          Message 4 of 7 , Jul 13, 2006
          • 0 Attachment
            > From your description, it is not clear if this will be part of a larger
            > script that you are developing. If all that you want to do is ability to
            > search and open the file, one of the existing plugins might be able to
            > help you.

            I'm adding a function to the potwiki script so that I can create a new
            wiki page based on a template.

            > But if you want to ask the user to select one and do something
            > with that selection, you can use the glob() and inputlist() functions to
            > do this, something like this:
            >
            > let files = glob(dir.'*Template')
            > if files != ''
            > let filelist = split(files, "\n")
            > let sel = inputlist(['Select file:']+filelist)
            > if sel != -1
            > let selectedFile = filelist[sel]
            > endif
            > endif

            This example worked very well for me. Thanks a ton!

            Tom Purl
          • Tom Purl
            ... Thanks a *ton* Tim! This looks very succinct (if not a bit frightening). I ll definitely try this one out too. ... Wow, I m glad that I started Vim
            Message 5 of 7 , Jul 13, 2006
            • 0 Attachment
              >> 1. Searches a directory for all files that end in the string "Template"
              >> 2. Return those file names to the user as chooseable list, similar to
              >> the way that the spell checker functionality works when you use the z=
              >> command.
              >
              > Well, as an ugly first-pass hack:
              >
              > :let s = expand("*template")
              > :echo substitute("\n".s."\n",
              > '^\%([^[:cntrl:]]*[[:cntrl:]]\)\{'.(confirm('Which file?',
              > s)).'}\([^[:cntrl:]]*\).*', '\1', 'g')
              >
              > Those two lines create a prompt with available matches for
              > whatever is stashed in "s".
              >
              > Instead of an :echo you can do an assignment, or make it the
              > return value of a function so you don't have to recreate this
              > beast repeatedly.

              Thanks a *ton* Tim! This looks very succinct (if not a bit
              frightening). I'll definitely try this one out too.

              > It can be wrapped in a function if desired. Hari's suggestion
              > was a good one, but requires Vim7 (at least it didn't work on 6.3
              > when I tried it because of the need for the inputlist() and
              > split() functions). As you reference the "like spell-checker",
              > you're likely running 7.0 and thus are safe with his much clearer
              > method.

              Wow, I'm glad that I started Vim scripting after version 7 was released,
              because it looks like it was much more difficult to script using version
              6.

              > The above two-liner, despite its brutish opacity, should work at
              > least back to 6.0.

              Thanks again!

              Tom Purl
            • stri ker
              Try ... contains an s is displayed in a menu. Good luck, Kevin
              Message 6 of 7 , Jul 14, 2006
              • 0 Attachment
                Try
                :set wildmenu

                I put the above line in my .vimrc file and now can do the following:

                :e *s* #then hit tab and any file in the current directory that
                contains an 's' is displayed in a menu.

                Good luck,
                Kevin


                On Jul 13, 2006, at 1:58 PM, Tom Purl wrote:

                > Hi, I'm a Vim scripting newbie, and had a pretty general question.
                >
                > I'm would like to write a function that does the following:
                >
                > 1. Searches a directory for all files that end in the string
                > "Template"
                > 2. Return those file names to the user as chooseable list, similar to
                > the way that the spell checker functionality works when you use the z=
                > command.
                >
                > Would anyone know how to do this? I checked the functions help in
                > Vim,
                > but couldn't find what I was looking for.
                >
                > Thanks in advance for any guidance!
                >
                > Tom Purl
                >
              • Tim Chase
                ... The 10k foot view is offer the user the list of files (the confirm() call) extract the Nth position from that list of files as determined by the index
                Message 7 of 7 , Jul 14, 2006
                • 0 Attachment
                  >>Well, as an ugly first-pass hack:
                  >>
                  >>:let s = expand("*template")
                  >>:echo substitute("\n".s."\n",
                  >>'^\%([^[:cntrl:]]*[[:cntrl:]]\)\{'.(confirm('Which file?',
                  >>s)).'}\([^[:cntrl:]]*\).*', '\1', 'g')
                  >
                  > Thanks a *ton* Tim! This looks very succinct (if not a bit
                  > frightening). I'll definitely try this one out too.

                  The 10k foot view is "offer the user the list of files (the
                  confirm() call) extract the Nth position from that list of files
                  as determined by the index returned from the confirm() call".

                  > Wow, I'm glad that I started Vim scripting after version 7 was released,
                  > because it looks like it was much more difficult to script using version
                  > 6.

                  Vim7 added lists (among other more advanced datatypes, IIRC)
                  which makes this hack considerably less taxing. The above hack
                  is a way of treating a string like a list, using the newline
                  characters (which happen to be control-characters, and thus match
                  the POSIX-type [[:cntrl:]] notation) as demarcations between list
                  items.

                  You could probably change the

                  "\n".s."\n"

                  to just

                  "\n".s

                  as, looking over it, it doesn't seem to require something
                  following the item of interest.

                  Hope this helps make a bit more sense of the chaos. :)

                  -tim
                Your message has been successfully submitted and would be delivered to recipients shortly.