ejr
ejr

Reputation: 120

Yank text between values defined with a global variable in Vim

The following command yanks the text delimited by ##'s and save it to buffer a:

:?##?;/##/y a

This looks for the first previous and next occurrences of the delimiter. But I want to generalize this to any delimiter. I was thinking about defining a variable with

let g:delimiter = "#another_delimiter"

But I don't know how to access that value to do something like

:?\=g:delimiter?;/\=g:delimiter/y a

Do you guys have any suggestion?

Upvotes: 1

Views: 202

Answers (1)

romainl
romainl

Reputation: 196751

If you are doing this on the command-line, you can insert the value of a variable with <C-r>=variable:

:?<C-r>=g:delimiter<CR>?,/<C-r>=g:delimiter<CR>/y a

If you are doing this in a script, you should use string concatenation instead:

execute ":?" . g:delimiter . "?,/" . g.delimiter . "/y a"

Upvotes: 2

Related Questions