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

How can I pass user input value to vimgrep?

Expand Messages
  • Pinaki
    I am trying to write a small script to mimic the Notepad++ s Find in Files feature. First it collects information from the user e.g. search item, directory
    Message 1 of 5 , May 21, 2014
    • 0 Attachment
      I am trying to write a small script to mimic the Notepad++ 's 'Find in Files'
      feature.
      First it collects information from the user e.g. search item, directory and
      file-type,
      then it should start performing.
      I got stumbled somewhere, after searching the Internet a lot
      I did not find any solution that worked in my case.
      Here is the detail:

      " ------------Find_in_Files.vim------------
      " -----------------------Implementation of a simple feature
      " -----------------------"Find in Files and Directories"
      " -----------------------like Notepad++ -----------------


      :menu Edit.Find\ in\ Files :<Esc>:<Esc>:call Find_in_Files() <Cr>

      function Find_in_Files()

      " -----------------------raw method-----------------
      " :vimgrep /GNU/ C:\CodeBlocks\TDM-GCC-64\include\*.txt
      " :cw
      " --------------------------------------------------

      let what2find = inputdialog("search term", "", "cancel pressed")
      let directory = browsedir("Browse the directory","")
      let filetype = inputdialog("filetype", "", "cancel pressed")
      if what2find != "" && what2find != "cancel pressed" && directory != ""
      && filetype != ""


      " :exe "!vimgrep /".what2find "\b/" " " .directory "\b\*." .filetype
      " not working
      :execute "vimgrep /".what2find "\b/" " " .directory "\b\*."
      .filetype
      :cw
      " also not working

      else
      "process if user press okay
      if what2find != "cancel pressed" && directory != "cancel pressed"
      && filetype != "cancel pressed"
      " I will repeat the same here if something goes well
      endif
      endif
      endfunction



      This plug-in is working partially, all the problem comes when I pass a
      variable value from the user input to vimgrep.
      If there is any fix to this problem please let me know.





      --
      View this message in context: http://vim.1045645.n5.nabble.com/How-can-I-pass-user-input-value-to-vimgrep-tp5721661.html
      Sent from the Vim - General mailing list archive at Nabble.com.

      --
      --
      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/d/optout.
    • Nikolay Pavlov
      ... Files ... and ... ... .filetype ... What are you trying to do with b here? At first occurrence of b it simply means that you search for literal
      Message 2 of 5 , May 21, 2014
      • 0 Attachment


        On May 21, 2014 11:46 PM, "Pinaki" <appugupta007@...> wrote:
        >
        > I am trying to write a small script to mimic the Notepad++ 's 'Find in Files'
        > feature.
        > First it collects information from the user e.g. search item, directory and
        > file-type,
        > then it should start performing.
        > I got stumbled somewhere, after searching the Internet a lot
        > I did not find any solution that worked in my case.
        > Here is the detail:
        >
        > " ------------Find_in_Files.vim------------
        > " -----------------------Implementation of a simple feature
        > " -----------------------"Find in Files and Directories"
        > " -----------------------like Notepad++ -----------------
        >
        >
        > :menu Edit.Find\ in\ Files  :<Esc>:<Esc>:call Find_in_Files() <Cr>
        >
        > function Find_in_Files()
        >
        > " -----------------------raw method-----------------
        > " :vimgrep /GNU/ C:\CodeBlocks\TDM-GCC-64\include\*.txt
        > " :cw
        > " --------------------------------------------------
        >
        >         let what2find = inputdialog("search term", "", "cancel pressed")
        >         let     directory       = browsedir("Browse the directory","")
        >         let filetype = inputdialog("filetype", "", "cancel pressed")
        >     if what2find != "" && what2find != "cancel pressed" && directory !=  ""
        > && filetype !=  ""
        >
        >
        >         " :exe "!vimgrep /".what2find "\b/" " " .directory "\b\*." .filetype
        > " not working
        >         :execute "vimgrep /".what2find "\b/" " " .directory "\b\*."
        > .filetype

        What are you trying to do with \b here? At first occurrence of \b it simply means that you search for literal CTRL-H that is preceded by what2find *followed by a space*. At second it means that you are searching for file with given extension and name starting with CTRL-H *in the current directory* and in file named directory. I think you must do the following:

        1. To get rid of unneeded spaces use concat operator (dot).
        2. Remove quoted space before directory: needed space is inserted by :execute (unless you use concat operator).
        3. Use fnameescape() to escape directory name.
        4. Additionally escape forward slash in what2find using escape(). Also make sure that what2find does not end with unescaped backward slash.
        5. Replace all string comparisons with ==# or ==?. Better with is# or is?. *Never* use == for string comparison, == depends on the value of &ignorecase option. is#/is? must be used if you expect operands to have different types and one of the operands definitely has scalar type. Though I just use them always for string comparison, even if I know for sure that both operands will be strings.

           Same is true for !=: use !=#/!=? or isnot#/isnot?. (Hash stands for 'noignorecase', ? stands for 'ignorecase'.)

        >         :cw
        >                   " also not working

        :cw does not work if there are no search results.

        >
        >     else
        >         "process if user press okay
        >         if what2find != "cancel pressed" && directory !=  "cancel pressed"
        > && filetype !=  "cancel pressed"
        >                " I will repeat the same here if something goes well
        >         endif
        >     endif
        > endfunction
        >
        >
        >
        > This plug-in is working partially, all the problem comes when I pass a
        > variable value from the user input to vimgrep.
        > If there is any fix to this problem please let me know.
        >
        >
        >
        >
        >
        > --
        > View this message in context: http://vim.1045645.n5.nabble.com/How-can-I-pass-user-input-value-to-vimgrep-tp5721661.html
        > Sent from the Vim - General mailing list archive at Nabble.com.
        >
        > --
        > --
        > 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/d/optout.

        --
        --
        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/d/optout.
      • Pinaki
        Thanks, following your advice finally the script started working. Here is the changed script: Last Change: 2014-05-22 Thursday: 04:16:18 PM
        Message 3 of 5 , May 22, 2014
        • 0 Attachment
          Thanks, following your advice finally the script started working.
          Here is the changed script:


          " Last Change: 2014-05-22 Thursday: 04:16:18 PM
          " -----------------------Implementation of a simple feature
          " -----------------------"Find in Files and Directories"
          " -----------------------like Notepad++ -----------------
          " -----------------------Created & Edited by Pinaki Sekhar Gupta
          <appugupta007@...>------------------------
          " -----------------------Licence: No licence is better than
          everything-------------------------------------------



          :menu Edit.Find\ in\ Files :<Esc>:<Esc>:call Find_in_Files() <Cr>

          function Find_in_Files()

          " -----------------------raw method-----------------
          " :vimgrep /GNU/ C:\CodeBlocks\TDM-GCC-64\include\*.txt
          " :cw
          " --------------------------------------------------

          let what2find = inputdialog("search term", "", "cancel pressed")
          let directory = browsedir("Browse the directory","")
          let filetype = inputdialog("filetype", "", "cancel pressed")
          let forwardslash = "/"
          let space = " "
          let wildcard = "*."
          if what2find != "" && what2find != "cancel pressed" && directory != ""
          && filetype != ""

          " port to test whether the nested strings are
          working or not
          "
          http://vim.1045645.n5.nabble.com/How-can-I-pass-user-input-value-to-vimgrep-td5721661.html
          " :echom "Hello, " . "world"
          " Hello, world "
          http://learnvimscriptthehardway.stevelosh.com/chapters/26.html
          " echo forwardslash . what2find . forwardslash . space .
          directory . wildcard . filetype

          :execute "vimgrep /" . what2find . forwardslash . space .
          fnameescape(directory) . wildcard . fnameescape(filetype)
          :cw

          else
          " process if user press okay
          if what2find != "cancel pressed" && directory != "cancel pressed"
          && filetype != "cancel pressed"

          :execute "vimgrep /" . what2find . forwardslash . space .
          fnameescape(directory) . wildcard . fnameescape(filetype)
          :cw

          endif
          endif
          endfunction











          If you still have any improvement suggestion please inform..





          --
          View this message in context: http://vim.1045645.n5.nabble.com/How-can-I-pass-user-input-value-to-vimgrep-tp5721661p5721667.html
          Sent from the Vim - General mailing list archive at Nabble.com.

          --
          --
          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/d/optout.
        • Nikolay Pavlov
          ... Strange indent here. ... ... http://vim.1045645.n5.nabble.com/How-can-I-pass-user-input-value-to-vimgrep-td5721661.html ... space . ... And here. ... It
          Message 4 of 5 , May 22, 2014
          • 0 Attachment


            On May 22, 2014 10:14 PM, "Pinaki" <appugupta007@...> wrote:
            >
            > Thanks, following your advice finally the script started working.
            > Here is the changed script:
            >
            >
            > " Last Change: 2014-05-22  Thursday: 04:16:18 PM
            > " -----------------------Implementation of a simple feature
            > " -----------------------"Find in Files and Directories"
            > " -----------------------like Notepad++ -----------------
            > " -----------------------Created & Edited by Pinaki Sekhar Gupta
            > <appugupta007@...>------------------------
            > " -----------------------Licence: No licence is better than
            > everything-------------------------------------------
            >
            >
            >
            > :menu Edit.Find\ in\ Files  :<Esc>:<Esc>:call Find_in_Files() <Cr>
            >
            > function Find_in_Files()
            >
            > " -----------------------raw method-----------------
            > " :vimgrep /GNU/ C:\CodeBlocks\TDM-GCC-64\include\*.txt
            > " :cw
            > " --------------------------------------------------
            >
            >         let what2find = inputdialog("search term", "", "cancel pressed")
            >         let     directory       = browsedir("Browse the directory","")
            >         let filetype = inputdialog("filetype", "", "cancel pressed")
            >         let forwardslash = "/"
            >         let space = " "
            >         let wildcard = "*."

            Strange indent here.

            >     if what2find != "" && what2find != "cancel pressed" && directory !=  ""
            > && filetype !=  ""
            >
            >                     "          port to test whether the nested strings are
            > working or not
            >                     "
            > http://vim.1045645.n5.nabble.com/How-can-I-pass-user-input-value-to-vimgrep-td5721661.html
            >                     " :echom "Hello, " . "world"
            >                     " Hello, world "
            > http://learnvimscriptthehardway.stevelosh.com/chapters/26.html
            >                     " echo forwardslash . what2find . forwardslash . space .
            > directory . wildcard . filetype

            And here.

            >         :execute "vimgrep /" . what2find . forwardslash . space .
            > fnameescape(directory) . wildcard . fnameescape(filetype)
            >         :cw
            >
            >     else
            >         " process if user press okay
            >         if what2find != "cancel pressed" && directory !=  "cancel pressed"
            > && filetype !=  "cancel pressed"
            >
            >             :execute "vimgrep /" . what2find . forwardslash . space .
            > fnameescape(directory) . wildcard . fnameescape(filetype)

            It is interesting why you use "forwardslash", "space" and "wildcard" variables? Concat operator has no problems with string literals on either side of itself. And do not use '!= "cancel pressed"'.

            Also why are you preceding some commands with a colon? I use it in two places: in *noremap definitions because it is required to enter command mode and after :autocmd/:command: as an element of coding style used to separate (auto)command body from the rest of arguments (preceding colon(s) are simply ignored so you may safely use them for various things like I do).

            >             :cw
            >
            >         endif
            >     endif
            > endfunction
            >
            >
            >
            >
            >
            >
            >
            >
            >
            >
            >
            > If you still have any improvement suggestion please inform..
            >
            >
            >
            >
            >
            > --
            > View this message in context: http://vim.1045645.n5.nabble.com/How-can-I-pass-user-input-value-to-vimgrep-tp5721661p5721667.html
            > Sent from the Vim - General mailing list archive at Nabble.com.
            >
            > --
            > --
            > 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/d/optout.

            --
            --
            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/d/optout.
          • Pinaki
            Actually backward/forward-slash, space were needed and the script was not working without them. Finally I decided to put them in their own places. However I
            Message 5 of 5 , May 23, 2014
            • 0 Attachment
              Actually backward/forward-slash, space were needed and the script was not
              working without them.
              Finally I decided to put them in their own places. However I changed my
              script a little and it is working as expected.
              Here is it:

              " Last Change: 2014-05-23 Friday: 12:03:38 PM
              " -----------------------Implementation of a simple feature
              " -----------------------"Find in Files and Directories"
              " -----------------------like Notepad++ -----------------
              " -----------------------Created & Edited by Pinaki Sekhar Gupta
              <appugupta007@...>------------------------
              " -----------------------Licence: No licence is better than
              everything-------------------------------------------



              :menu Edit.Find\ in\ Files :<Esc>:<Esc>:call Find_in_Files() <Cr>

              function Find_in_Files()

              " -----------------------raw method-----------------
              " :vimgrep /GNU/ C:\CodeBlocks\TDM-GCC-64\include\*.txt
              " :cw
              " --------------------------------------------------

              let what2find = inputdialog("search term", "", "cancel pressed")
              let directory = browsedir("Browse the directory","")
              let filetype = inputdialog("filetype", "", "cancel pressed")
              let forwardslash = "/"
              let space = " "

              if has("win32") || has("win16") || has("win64")
              " ========== MS Windows ===========
              let wildcard = "\\*."
              elseif has("macunix") || has("unix") || has("mac") || has("os2") ||
              has("beos")
              " ========== Linux/OS-X/Unix ===========
              let wildcard = "\/*."
              endif

              if what2find != "" && what2find != "cancel pressed" && directory != ""
              && filetype != ""

              " port to test whether the nested strings are
              working or not
              "
              http://vim.1045645.n5.nabble.com/How-can-I-pass-user-input-value-to-vimgrep-td5721661.html
              " :echom "Hello, " . "world"
              " Hello, world "
              http://learnvimscriptthehardway.stevelosh.com/chapters/26.html
              " echo forwardslash . what2find . forwardslash . space .
              directory . wildcard . filetype

              :execute "vimgrep /" . what2find . forwardslash . space .
              fnameescape(directory) . wildcard . filetype
              :cw

              else
              " process if user press okay
              if what2find != "cancel pressed" && directory != "cancel pressed"
              && filetype != "cancel pressed"

              :execute "vimgrep /" . what2find . forwardslash . space .
              fnameescape(directory) . wildcard . filetype
              :cw

              endif
              endif
              endfunction

              I am still looking for an improvement suggestion so that it can search the
              directory recursively, and more interestingly I want to look into the
              extension-less files like C++'s 'iostream' etc, and Linux created text file
              that does not have extension unless I give one.





              --
              View this message in context: http://vim.1045645.n5.nabble.com/How-can-I-pass-user-input-value-to-vimgrep-tp5721661p5721684.html
              Sent from the Vim - General mailing list archive at Nabble.com.

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