Delete / cut everything between two templates (download for registration) vim

I need to delete everything between two patterns or rather cut this out and put this ot register for the last use (put it in another file)

%s/\V<se.\{-}<\/sel//gc

but this is just a deletion (and it didn’t work, and I want to cut it off more quickly and put it in a register (maybe I need a script)

+4
source share
3 answers

Using a global team will work.

let @a=''|g/startpattern/.+1,/stoppattern/-1 delete A

Where

let @a=''                    - Clear the a register
g/startpattern/              - search for the start of the pattern
.+1,/stoppattern/-1 delete A - delete everything between start and stop and 
                               append it to register a

works for

startpattern
this goes to register A
so does this
stoppattern

but not for

startpattern this should go to register A stoppattern.
+1
source

I don’t know exactly which two templates you want, but try this line:

s/pattern1\zs\_.\{-}\zepattern2/\=setreg('x',submatch(0))>-1:'':''/
  • here are two patterns: pattern1andpattern2
  • ( ) , .
  • "" x
+2

Assuming you have a pattern that exactly matches what you want to “cut out,” just search for that pattern with a normal search /. Then you can use the facility gnto search for the removal action for the subsequent insertion of, for example dgn.

If you do not have this pattern, but you have a start and end regex, just place the cursor at the end of the "start" pattern (for example, using /pattern/e), and then delete until the search, for example d/pattern2.

0
source

Source: https://habr.com/ru/post/1570139/


All Articles