Search & replace with grep

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Search & replace with grep

mstep.germany


Hello all!


I switched from Mac BBEdit to vim and need help with a grep pattern. I am still a beginner and putting up a VIM cheat file in LATEX for all vim-commands, which are part of my "repertoire". My search pattern is following, and there are many of them in my file:

Search:

\begin{center}
  \textcolor{mygray}{\large w}
\end{center}

Replace:

   \vimcode{w}

My try was the following:

:s/\begin{center}\n\(\s\+\)\textcolor{mygray}{\large \([^}]\+\)}\s+\end{center}\s+/$1\vimcode{$2}/g

Do I have to escape all \begin with \\begin ? This would be cumbersome and a reason to give up VIM. Already to escape the capturing parenthesis \( \) you have to get used to.

Is there a possibility to visual select some lines, to edit them in the search dialogue? Would it be possible, to put visual  selected lines into the search dialogue with all special signs escaped automatically?


Thank you in advance


marek

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Search & replace with grep

JohnBeckett
On Sunday, April 1, 2018 at 8:29:47 PM UTC+10, mstep.germany wrote:
> Search:
>
> \begin{center}
>   \textcolor{mygray}{\large w}
> \end{center}

To search for visually selected text, see http://vim.wikia.com/wiki/VimTip171

I use the two vnoremap commands after "Search for selected text, forwards or backwards".

John

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Search & replace with grep

mstep.germany
On Monday, 2 April 2018 06:11:20 UTC+2, JohnBeckett  wrote:

> On Sunday, April 1, 2018 at 8:29:47 PM UTC+10, mstep.germany wrote:
> > Search:
> >
> > \begin{center}
> >   \textcolor{mygray}{\large w}
> > \end{center}
>
> To search for visually selected text, see http://vim.wikia.com/wiki/VimTip171
>
> I use the two vnoremap commands after "Search for selected text, forwards or backwards".
>
> John
Thank you John for your answer! I appreciate your answer even more, that you took the time for it in Easter Holidays. (<- sounds not like a good English idiom? Probably it's a French expression?).

Something I am doing wrong.

1. I put two lines in my .vimrc file:
    a) vnoremap // y/<C-R>"<CR>
    b) vnoremap <expr> // 'y/\V'.escape(@",'\').'<CR>'

I visual select the lines in my file to edit:


\begin{center}
  \textcolor{mygray}{\large W}
\end{center}

I enter //

Result: /\V\\begin{center} in the status (command?) line. That means this is the first line from my visual selection only. Did I do something wrong?

What I need is the following:

I visual select the lines to search for, I hit a command (which one?), and in the status line I have my search pattern automatically with escaped special characters like follows:

:s/\\begin\{center\}\n  \\textcolor\{mygray\}\{\\large W\}\n\\end\{center\}\n//

So that I can edit the search pattern in the status line like follows:

:%s/\\begin\{center\}(\s+)\\textcolor\{mygray\}\{\\large ([^}]+)\}\s+\\end\{center}\s+/$1\\vimcode{$2}\n/g

Would this be possible?

Best greetings to all


marek

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Search & replace with grep

mstep.germany
In reply to this post by mstep.germany
On Sunday, 1 April 2018 12:29:47 UTC+2, [hidden email]  wrote:

Sorry I forgot to escape the capturing \( \)

So my search pattern should be:

:s/\begin{center}\n\(\s\+\)\textcolor{mygray}{\large \([^}]\+\)}\s+\end{center}\s+/$1\vimcode{$2}/g

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Search & replace with grep

JohnBeckett
You are trying to do something difficult which requires a lot of background. I can only offer some thoughts.

Use the part of the tip that I mentioned: two vnoremap commands after "Search for selected text, forwards or backwards". Do not use anything else.

\_s means space or tab or newline.

Visually select the three lines then press *

If you wanted to replace it with something simple you could do:

:%s//REPLACE/g

You want to add a capture so you would need to type:

    :%s/
    Ctrl-R /
    /REPLACE/g

That is all in one line in the command line. The two keys Ctrl-R / inserts the value of the search register.

Before pressing Enter to execute the command, you have to edit it to insert the capture. I suggest typing the command into a temporary buffer where you can take your time to get it right. When finished you would have something like:

:%s/SEARCH/REPLACE/g

With the cursor on the line press Y to copy the whole line. Then switch to the buffer where you want to make the change and type @"

Typing @x executes the contents of register x. @" is the unnamed register (:help registers).

In this case the "g" flag (global) is not needed because there can only be one hit per line.

If you try all this and can't get the command to work, post the command you used.

John

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Search & replace with grep

mstep.germany
In reply to this post by mstep.germany

Thank you again John!

This was a long and laborious answer. Respect!

I will study your hints attentively later. But I think I will return to BBEdit, if I have a global grep search & replace.

For the rest, I get accustomed more and more to the handy features of VIM.


Best greetings


marek

--
--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.