5

Is there any way to get data with in braces, before a certain latex command (\body) and assign that content (long text) to a variable.

eg:

\text{just a text before body} \body{contains lot of paragraphs etc etc etc, and that paragraphs also contains lot of latex command like \textbf{my name} and \textit{text} etc. but i want all the content with in the brace} \text{just a text after body}

i need

\body{contains lot of paragraphs etc etc etc, and that paragraphs also contains lot of latex command like \textbf{my name} and \textit{text} etc. but i want all the content with in the brace} in a variable

I want some search and replace in it. thats why

i did a macro to yank text with in braces by the help of % (to search with in braces).

is there any easy way to do this?

thanks in advance

Zam
  • 367
  • 2
  • 17
  • 1
    Your example text is rather unclear. Is the "but I want ..." part of the text, or part of the question? Use code formatting instead of quotes for sample input, please. Your example output also doesn't have the `\text{..}` part, so do you want to copy or delete? – muru Feb 26 '16 at 04:47

1 Answers1

6

You can assign everything inside the { to register a using

"ayi{

Breakdown is

"a - select register a
y  - yank 
i{ - everything inside {}

It does the right thing for matching braces

If you later need to access the contents of a you can do this in multiple different ways depending on the context - I think the ways are

"ap    : paste register a in normal mode
<C-r>a : paste register a into command line
@a     : access register in script

It's worth noting that regex is not powerful enough to do matching braces. (I think there are extensions that can make it powerful enough, but I'm not sure if vim supports any of those),

Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
  • it works, when i place cursor at position "{" in "\body{". i need a script to woks the requirement even cursor is at start of the file. because i need to add that script in my vimscript (do, blindly) – Zam Feb 26 '16 at 05:05
  • Is it enough to navigate to the top of the file then find the `\body{` part and place the cursor their first? - you can do that via `gg/\\body{/e`, so the whole lot would be `gg/\\body{/e"ayi{` if you also need the surrounding `{` and the `\body` section you could do `gg/\\body{/eva{oF\"ay` which will expand the selected region before yanking. – Michael Anderson Feb 26 '16 at 05:16