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

Re: If I copy multi line text to clipboard how do I know use that in vim for replace text in search/replace?

Expand Messages
  • Rick R
    ... I m still having trouble with this. I actually want it to replace the text in multiple files that aren t opened. I thought about trying to use sed but for
    Message 1 of 7 , Dec 1, 2011
    • 0 Attachment
      On Thu, Dec 1, 2011 at 11:20 PM, Tim Chase <vim@...> wrote:
      On 12/01/11 22:11, Rick R wrote:
      I often will find a multi line snippet of text that I'd like
      to then replace in multiple files in my project after a
      certain block of text (maybe it's some javascript for example
      so I'll want the multiple lines pasted after the
      initial<script>  tag.)

      How do I do this easily in vim (or MacVim/gVim if those gui
      editors on top can help?)

      Without particulars, it's a bit hard to give a concrete example.  In the general case, it sounds like you want to mix a combination of an argdo/windo/bufdo/tabdo command (to iterate over all the associated buffers/windows) and issue a search&replace (or insertion) command anchored at at a given text.  Thus you might have something like

       :windo %s/block_of_text\zsmulti\nline\ntext/replacement

      or

       :set hidden
       :bufdo g/block_of_text/sil! put='some text to put after'
       (verify it all looks good)
       :wall

      If you have content in the clipboard you want to paste after each line, you can do

       :[whatever]do g/where_to_put_it/put=@*


      I'm still having trouble with this. I actually want it to replace the text in multiple files that aren't opened. I thought about trying to use sed but for the life of me I can't figure out how to get the replace/append portion of text to work with multiple lines of the text I want to replace with? For example if I have the following text in an html file:

      <body>

      Now I have multiple lines I want to add after that I've copied from a website.....
      foo
      bar
      foo
      bar

      I want to append them after <body> in all the html files in the directory.

      With sed I couldn't figure out (from googling) how I could replace the multiple line text that I have... as soon as i'd paste that into the terminal it would obviously cause line breaks. 

      Using what you suggested in vim I though maybe I could do something like:

      :args *.html
      :argdo g/<body>/put=@*

      But when try that (and I know in the above I'd lose the <body> tag if it worked) all I get is <body> highlighted in the file displayed.

      Any more suggestions appreciated. 



       
      The reason for the 'hidden' is that Vim won't let you leave a modified buffer unless it's set.  With :windo or :tabdo it's not a problem because they aren't closed, but with argdo/bufdo, they leave the current (modified) buffer to progress to the next.  If you're feeling reckless, you can include a ":w" after your command to also write the file out before leaving it (with the caveats listed at ":help :bar" regarding commands that may require an :exec )

      Hope this points you in the right direction.  With greater detail from you, perhaps more detailed help can be given :)

      -tim






      --
      Rick R

      --
      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
    • Albin Olsson
      ... You should not lose the body tag because put will not replace anything. I m guessing that you are on Linux, in that case the * register is the selection
      Message 2 of 7 , Dec 2, 2011
      • 0 Attachment
        On Fri, Dec 2, 2011 at 6:49 AM, Rick R <rickcr@...> wrote:
        >
        > Using what you suggested in vim I though maybe I could do something like:
        >
        > :args *.html
        > :argdo g/<body>/put=@*
        >
        > But when try that (and I know in the above I'd lose the <body> tag if it
        > worked) all I get is <body> highlighted in the file displayed.

        You should not lose the body tag because put will not replace anything.
        I'm guessing that you are on Linux, in that case the * register is the
        selection clipboard and and + is the copied text clipboard.
        So try

        :argdo g/<body>/put +

        instead.

        Hope it helps.


        --
        Albin Olsson, IT Consultant and Classic Connoisseur
        www.albinolsson.se | albin.olsson@...
        +46 707 831 830

        --
        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
      • Tim Chase
        ... If all you want to do is put saved content after a tag, and assuming the tag stands alone on the line & has no additional attributes, the sed
        Message 3 of 7 , Dec 2, 2011
        • 0 Attachment
          On 12/01/11 23:49, Rick R wrote:
          > On Thu, Dec 1, 2011 at 11:20 PM, Tim Chase<vim@...> wrote:
          >> If you have content in the clipboard you want to paste after each line,
          >> you can do
          >>
          >> :[whatever]do g/where_to_put_it/put=@*
          >>
          >>
          > I'm still having trouble with this. I actually want it to replace the text
          > in multiple files that aren't opened. I thought about trying to use sed but
          > for the life of me I can't figure out how to get the replace/append portion
          > of text to work with multiple lines of the text I want to replace with? For
          > example if I have the following text in an html file:
          >
          > <body>
          >
          > Now I have multiple lines I want to add after that I've copied from a
          > website.....
          > foo
          > bar
          > foo
          > bar
          >
          > I want to append them after<body> in all the html files in the directory.
          >
          > With sed I couldn't figure out (from googling) how I could replace the
          > multiple line text that I have... as soon as i'd paste that into the
          > terminal it would obviously cause line breaks.

          If all you want to do is put saved content after a <body> tag,
          and assuming the <body> tag stands alone on the line & has no
          additional attributes, the sed is pretty easy:

          sed -i.bak '/<body>/r mytext.txt' *.html

          This will create .bak backup files for each of the files it
          modifies in-place. The vim equivalent would be almost the same:

          vim *.html
          :set hidden
          :argdo g/<body>/r mytext.txt
          (evaluate, and if all's what you want)
          :wall

          > :args *.html
          > :argdo g/<body>/put=@*
          >
          > But when try that (and I know in the above I'd lose the<body> tag if it
          > worked) all I get is<body> highlighted in the file displayed.

          I think Albin beat me to the answer, but if you're on X, there's
          a selection register ("*", usually pasted with the middle-mouse)
          and the clipboard register ("+", managed with control+C/V/X or
          the Copy/Cut/Paste menu options in other applications).

          It helps to know what the commands are doing so you can
          understand how to tweak it in the future:

          :argdo " for each of the file-arguments
          g/<body>/ " on every line matching this pattern
          issue one of the following commands:
          r mytext.txt " read the contents of "mytext.txt" below
          put + " put the contents of the clipboard
          put=@+ " put the contents of evaluating an expression
          (in this case, the same as the previous
          but can be more complex evaluation calling
          functions or performing logic)

          -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
        • Ben Fritz
          ... Then, the best way to do it, is using :argdo. ... (:args will also work above but will mess with the global argument list) ... Note your incorrect use of
          Message 4 of 7 , Dec 2, 2011
          • 0 Attachment
            On Dec 1, 11:49 pm, Rick R <ric...@...> wrote:
            >
            > I'm still having trouble with this. I actually want it to replace the text
            > in multiple files that aren't opened.

            Then, the best way to do it, is using :argdo.

            Start by opening a new window. The execute:

            :arglocal (pattern or list of files to add)
            (:args will also work above but will mess with the global argument
            list)
            :argdo g/search pattern/put {register name}

            Or, for your example:
            :args *.html
            :argdo g/<body>/put *

            > [snip]
            >
            > Using what you suggested in vim I though maybe I could do something like:
            >
            > :args *.html
            > :argdo g/<body>/put=@*
            >

            Note your incorrect use of the expression register, instead of the
            register you actually want.

            > But when try that (and I know in the above I'd lose the <body> tag if it
            > worked) all I get is <body> highlighted in the file displayed.
            >

            You should not lose the <body> tag in this situation. The :g command
            does not do a replace, it only performs an action on lines that match.
            The action performed in this case, is "paste the contents of register
            * below the matched line".

            I'm just now re-reading Albin's response, and seeing that he said much
            the same thing in fewer words. Sorry for any duplication but maybe
            multiple perspectives will help your understanding.

            --
            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
          • Rick R
            ... (Thanks to everyone in this thread for replying.. I don t mind multiple replies for different perspectives.) Have to run to a meeting so only time to reply
            Message 5 of 7 , Dec 2, 2011
            • 0 Attachment
              On Fri, Dec 2, 2011 at 3:35 AM, Albin Olsson <albin.olsson@...> wrote:
               
              You should not lose the body tag because put will not replace anything.
              I'm guessing that you are on Linux, in that case the * register is the
              selection clipboard and and + is the copied text clipboard.
              So try

              :argdo g/<body>/put +

              instead.

              Hope it helps.
               

              (Thanks to everyone in this thread for replying.. I don't mind multiple replies for different perspectives.)

              Have to run to a meeting so only time to reply quickly to the above.
              When I tried that I ended up getting this response:


              If I hit enter I don't see anything changed. 

              The text in the clipboard I had copied directly from one of these gmail messages just for testing purposes (just copied a few lines of text.)

              Does it matter that I'm on the MacOS? I'll try it in my Ubuntu virtual to see it makes a difference.


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