Anu Mohan
Anu Mohan

Reputation: 83

How to get string between two commands in vim

I'm trying to write a vim script for get string between two commands. eg: \string{new strings}, I want to get the new string if it contain empty lines or space

:%s/\\string{[^}]*\n*[^}]*}/new/gec

Upvotes: 3

Views: 315

Answers (2)

Kent
Kent

Reputation: 195039

Your requirement is not that clear, but if you want to

get the "new string" if it contain empty lines or space

Also you commented:

select the contents inside the curly brace of \string{} even if there is any space or line

This line does it, it is a search command, not :s

/\\string{\zs\_[^}]*

If you want to do some substitution on the content between \string{ and }, you can use the pattern:

%s/\\string{\zs\_[^}]*\ze}/whatever/g

Note that, you can also write s/\\string{\zs\_[^}]*/whatever/g, the \ze} will make sure that the closing bracket must be there. Not sure if this is needed.

For the detail of \_[], do a :h \_[

Upvotes: 2

Zam
Zam

Reputation: 377

use this code

%s/\\string{[^}]*\n*[^}]*\n*[^}]*\n*[^}]*\n*[^}]*\n*[^}]*\n*[^}]*\n*[^}]*\n*[^}]*\n*[^}]*\n*[^}]*\n*[^}]*\n*[^}]*\n*[^}]*\n*[^}]*\n*[^}]*\n*[^}]*\n*[^}]*\n*[^}]
*}/new/gec 

Upvotes: 0

Related Questions