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

S&R with search input from a file

Expand Messages
  • Jeri Raye
    Hi, Is it posible to search for a list of words, from a file and replace them if found in the buffer that is open, and making the first letter a uppercase For
    Message 1 of 7 , Apr 18, 2014
    • 0 Attachment
      Hi,

      Is it posible to search for a list of words, from a file and replace them if found in the buffer that is open, and making the first letter a uppercase
      For example:

      FileNAME: INPUT.TXT
      Containing:
      +----------+
      james
      quebec
      canada
      +----------+


      :%s/<INPUT.TXT>/replace found word with same word but first letter in uppercase./g


      Example:
      james was in quebec.
      The city quebec is in canada.

      Becomes:
      James was in Quebec.
      The city Quebec is in Canada.

      Rgds, Jeri

      --
      --
      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.
    • Tim Chase
      ... You can transform your input file into a single command: vi data.txt ... 0y$ yank the newly-formed regexp ... substitute across the
      Message 2 of 7 , Apr 18, 2014
      • 0 Attachment
        On 2014-04-18 20:07, Jeri Raye wrote:
        > FileNAME: INPUT.TXT
        > Containing:
        > +----------+
        > james
        > quebec
        > canada
        > +----------+
        >
        >
        > :%s/<INPUT.TXT>/replace found word with same word but first letter
        > in uppercase./g
        >
        >
        > Example:
        > james was in quebec.
        > The city quebec is in canada.
        >
        > Becomes:
        > James was in Quebec.
        > The city Quebec is in Canada.

        You can transform your input file into a single command:

        vi data.txt
        :sp input.txt " open your word-list in a new window
        :%s/$/\\|/ " start transforming it into a regexp
        :%j! " join each line together
        :s/..$ " delete the "\|" from the end of the line
        0y$ " yank the newly-formed regexp
        :wincmd w " flip back to the file you want to change
        :%s/\c\<\(<c-r>0\)\>/\u&/g
        " substitute across the entire file (":%s/")
        " ignoring case ("\c")
        " a word must start here to match ("\<")
        " start the list of alternatives ("\(")
        " use control+R followed by zero
        " to include the word-list joined by \|
        " that we yanked previously
        " close the list of alternatives ("\)")
        " ensure that the word ends here ("\>")
        " and replace it with ("/")
        " the next letter uppercased ("\u")
        " the text we captured ("&")
        " and do all replacements on the line ("/g")

        This assumes that your input list is all just whole words, no funky
        regexp metachars (periods, backslashes, asterisks, or
        open-square-brackets come to mind).

        -tim



        --
        --
        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.
      • Jeri Raye
        Hi Tim, I get as far as the following ... doesn t work at my windows system I use: execute normal! xx Then it fails on 0y$ I get the error message
        Message 3 of 7 , Apr 20, 2014
        • 0 Attachment
          Hi Tim,

          I get as far as the following
          :sp input.txt    
          :%s/$/\\|/        
          :%j!             


          :s/..$     
          doesn't work at my windows system
          I use:      
          execute "normal! \<end>xx"

          Then it fails on
          0y$              

          I get the error message E488: Trailing characters

          I also noticed that the <C-R> mapping in the last substitute command is already used in another plugin.
          Is there a workaround for?

          Rgds,
          Jeri


          On Fri, Apr 18, 2014 at 9:44 PM, Tim Chase <vim@...> wrote:
          On 2014-04-18 20:07, Jeri Raye wrote:
          > FileNAME: INPUT.TXT
          > Containing:
          > +----------+
          > james
          > quebec
          > canada
          > +----------+
          >
          >
          > :%s/<INPUT.TXT>/replace found word with same word but first letter
          > in uppercase./g
          >
          >
          > Example:
          > james was in quebec.
          > The city quebec is in canada.
          >
          > Becomes:
          > James was in Quebec.
          > The city Quebec is in Canada.

          You can transform your input file into a single command:

          vi data.txt
          :sp input.txt      " open your word-list in a new window
          :%s/$/\\|/         " start transforming it into a regexp
          :%j!               " join each line together
          :s/..$             " delete the "\|" from the end of the line
          0y$                " yank the newly-formed regexp
          :wincmd w          " flip back to the file you want to change
          :%s/\c\<\(<c-r>0\)\>/\u&/g
                             " substitute across the entire file (":%s/")
                             " ignoring case ("\c")
                             " a word must start here to match ("\<")
                             " start the list of alternatives ("\(")
                             " use control+R followed by zero
                             "    to include the word-list joined by \|
                             "    that we yanked previously
                             " close the list of alternatives ("\)")
                             " ensure that the word ends here ("\>")
                             " and replace it with ("/")
                             " the next letter uppercased ("\u")
                             " the text we captured ("&")
                             " and do all replacements on the line ("/g")

          This assumes that your input list is all just whole words, no funky
          regexp metachars (periods, backslashes, asterisks, or
          open-square-brackets come to mind).

          -tim




          --
          --
          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.
        • Tim Chase
          ... This should work on all versions of vim, regardless of operating system. How are you determining that it isn t working? Is it giving you an error? Is it
          Message 4 of 7 , Apr 21, 2014
          • 0 Attachment
            On 2014-04-21 08:37, Jeri Raye wrote:
            > :s/..$
            > doesn't work at my windows system
            > I use:
            > execute "normal! \<end>xx"

            This should work on all versions of vim, regardless of operating
            system. How are you determining that it isn't working? Is it giving
            you an error? Is it not removing the last two characters (the "\|")
            from the line? If you are having a problem, you could tweak the
            first couple steps to

            :1,$-s/$/\\|/

            which will tack them onto every line *except* the last one, saving
            you that ":s/..$/" step.

            > Then it fails on
            > 0y$
            >
            > I get the error message E488: Trailing characters

            This sounds like you're trying to do this in some mode that isn't
            normal-mode. Perhaps command-mode?

            You could yank the entire line and then just remove the newline at
            the end if that's easier.

            > I also noticed that the <C-R> mapping in the last substitute
            > command is already used in another plugin.
            > Is there a workaround for?

            You mean...other than not remapping away something that is incredibly
            valuable? ;-) You can always map something else to it:

            :cnoremap <f4> <c-r>

            -tim




            --
            --
            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.
          • Jeri Raye
            Hi Tim, I m trying to make a function out of your commands. Perhaps that that is the reason why it s not working straight away. Rgds, Jeri ... -- -- You
            Message 5 of 7 , Apr 23, 2014
            • 0 Attachment
              Hi Tim,

              I'm trying to make a function out of your commands.
              Perhaps that that is the reason why it's not working straight away.

              Rgds, Jeri


              On Mon, Apr 21, 2014 at 4:42 PM, Tim Chase <vim@...> wrote:
              On 2014-04-21 08:37, Jeri Raye wrote:
              > :s/..$
              > doesn't work at my windows system
              > I use:
              > execute "normal! \<end>xx"

              This should work on all versions of vim, regardless of operating
              system.  How are you determining that it isn't working?  Is it giving
              you an error?  Is it not removing the last two characters (the "\|")
              from the line?  If you are having a problem, you could tweak the
              first couple steps to

                :1,$-s/$/\\|/

              which will tack them onto every line *except* the last one, saving
              you that ":s/..$/" step.

              > Then it fails on
              > 0y$
              >
              > I get the error message E488: Trailing characters

              This sounds like you're trying to do this in some mode that isn't
              normal-mode.  Perhaps command-mode?

              You could yank the entire line and then just remove the newline at
              the end if that's easier.

              > I also noticed that the <C-R> mapping in the last substitute
              > command is already used in another plugin.
              > Is there a workaround for?

              You mean...other than not remapping away something that is incredibly
              valuable?  ;-)  You can always map something else to it:

                :cnoremap <f4> <c-r>

              -tim




              --
              --
              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.
            • Jeri Raye
              Hi Ti, I ve removed the mapping that was misusing the default vim behaviour. I ve reworked your code to the function below. ... - I see my input list
              Message 6 of 7 , Apr 27, 2014
              • 0 Attachment
                Hi Ti,

                I've removed the mapping that was misusing the default vim <C-R> behaviour.

                I've reworked your code to the function below.

                When I now do:
                :call SRT_CorrectNames()
                - I see my input list transformed to a joined line with \| between the words
                - I see that I move to my file where I want to do substitution.
                - I now get an error on the substitute
                E486: pattern not found : \c\<\(<C-R>0\)\>

                Any idea why pattern isn't found?

                function! SRT_CorrectNames()
                :sp input.txt    
                :%s/$/\\|/        
                :%j!             
                ":s/..$            
                execute "normal! \<end>xx"

                "0y$              
                        let @0 = getline(1)
                        di 0

                "
                :wincmd w        
                %s/\c\<\(<C-R>0\)\>/\u&/g
                "                   " substitute across the entire file (":%s/")
                "                   " ignoring case ("\c")
                "                   " a word must start here to match ("\<")
                "                   " start the list of alternatives ("\(")
                "                   " use control+R followed by zero
                "                   "    to include the word-list joined by \|
                "                   "    that we yanked previously
                "                   " close the list of alternatives ("\)")
                "                   " ensure that the word ends here ("\>")
                "                   " and replace it with ("/")
                "                   " the next letter uppercased ("\u")
                "                   " the text we captured ("&")
                "                   " and do all replacements on the line ("/g")
                "
                "This assumes that your input list is all just whole words, no funky
                "regexp metachars (periods, backslashes, asterisks, or
                "open-square-brackets come to mind).endfunction
                endfunction

                Rgds,
                Jeri



                On Wed, Apr 23, 2014 at 7:58 PM, Jeri Raye <jeri.raye@...> wrote:
                Hi Tim,

                I'm trying to make a function out of your commands.
                Perhaps that that is the reason why it's not working straight away.

                Rgds, Jeri


                On Mon, Apr 21, 2014 at 4:42 PM, Tim Chase <vim@...> wrote:
                On 2014-04-21 08:37, Jeri Raye wrote:
                > :s/..$
                > doesn't work at my windows system
                > I use:
                > execute "normal! \<end>xx"

                This should work on all versions of vim, regardless of operating
                system.  How are you determining that it isn't working?  Is it giving
                you an error?  Is it not removing the last two characters (the "\|")
                from the line?  If you are having a problem, you could tweak the
                first couple steps to

                  :1,$-s/$/\\|/

                which will tack them onto every line *except* the last one, saving
                you that ":s/..$/" step.

                > Then it fails on
                > 0y$
                >
                > I get the error message E488: Trailing characters

                This sounds like you're trying to do this in some mode that isn't
                normal-mode.  Perhaps command-mode?

                You could yank the entire line and then just remove the newline at
                the end if that's easier.

                > I also noticed that the <C-R> mapping in the last substitute
                > command is already used in another plugin.
                > Is there a workaround for?

                You mean...other than not remapping away something that is incredibly
                valuable?  ;-)  You can always map something else to it:

                  :cnoremap <f4> <c-r>

                -tim




                --
                --
                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.
              • Tim Chase
                ... It seems to be thinking that the is a literal, not a command to insert a register. You can try the following, all done as Ex commands rather than ...
                Message 7 of 7 , May 5, 2014
                • 0 Attachment
                  On 2014-04-27 09:35, Jeri Raye wrote:
                  > E486: pattern not found : \c\<\(<C-R>0\)\>
                  >
                  > Any idea why pattern isn't found?

                  It seems to be thinking that the <C-R> is a literal, not a
                  command to insert a register.

                  You can try the following, all done as Ex commands rather than
                  normal-mode commands:

                  :sview data.txt
                  :%y a
                  :let @/='\<\('.substitute(substitute(@a,'\n','\\|','g'), '..$',
                  '', '').'\)\>\c'
                  :q
                  :%s//\u&/g

                  This should

                  1) split "data.txt" in a new window
                  2) yank the whole thing into register "a"
                  3) make your search register become the transformation of the content
                  in register-a into the desired search query (wrapped in "\<...\>")
                  4) close the data.txt window
                  5) perform the substitution across your entire file, replacing the
                  first letter with its uppercase variant

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