How to use variables in command-line mode searching

9 views
Skip to first unread message

Pento

unread,
Aug 6, 2009, 8:49:09 AM8/6/09
to vim_use
Hello, everybody!

I have interesting question.
Is it possible to use predefined variables in command-line mode
searching in vim?
Example:
we have some text like " bla-bla bar"

:let a="foo"
:%s/bar/$a/g

Result:
" bla-bla foo"

Teemu Likonen

unread,
Aug 6, 2009, 9:04:44 AM8/6/09
to vim...@googlegroups.com
On 2009-08-06 05:49 (-0700), Pento wrote:

> Is it possible to use predefined variables in command-line mode
> searching in vim?
> Example:
> we have some text like " bla-bla bar"
>
> :let a="foo"
> :%s/bar/$a/g
>
> Result:
> " bla-bla foo"

Interactively you can use "Ctrl-r =" and then type any expression to be
evaluated. Type "a" and then Enter to have variable "a" evaluated, like
this:

:%s/bar/<Ctrl-r>=a<Enter>/g

Here <Ctrl-r> and <Enter> mean that you really press those keys.

You can also use :execute command to evaluate expressions and have the
result of evaluation become the command to run, like:

execute '%s/var/' . a . '/g'

sergio

unread,
Aug 6, 2009, 9:59:58 AM8/6/09
to vim_use
let @a="word"

:%s/word/\=@a/g

\=@a is reg 'a' :)

sergio

unread,
Aug 6, 2009, 10:07:18 AM8/6/09
to vim_use
let @a="new"

%s/old/\=@a/g

John Beckett

unread,
Aug 6, 2009, 8:25:43 PM8/6/09
to vim...@googlegroups.com
Please bottom post on this list. Quote a small (relevant) part
of the message you are replying to, and put your text underneath.

See the list guidelines:
http://groups.google.com/group/vim_use/web/vim-information

KKde

unread,
Aug 7, 2009, 2:57:07 AM8/7/09
to vim_use

>
> Interactively you can use "Ctrl-r =" and then type any expression to be
> evaluated. Type "a" and then Enter to have variable "a" evaluated, like
> this:
>
>     :%s/bar/<Ctrl-r>=a<Enter>/g
>
> Here <Ctrl-r> and <Enter> mean that you really press those keys.

How could I enter <Ctrl-r> in command line.. I tried directly by
pressing those kesy in command line..But it's not working... Can you
let me know what wrong I am doing here?

Thanks,
Sharat

John Beckett

unread,
Aug 7, 2009, 7:45:24 AM8/7/09
to vim...@googlegroups.com
KKde wrote:
> How could I enter <Ctrl-r> in command line.. I tried directly
> by pressing those kesy in command line.

In normal mode (after pressing Esc), type the following:

:echo<space><ctrl-r>=2+3<enter><enter>

The "=2+3" is replaced with "5". You could also have entered
"=x" where x is a variable.

John

KKde

unread,
Aug 8, 2009, 12:40:24 AM8/8/09
to vim_use



> In normal mode (after pressing Esc), type the following:
>
>   :echo<space><ctrl-r>=2+3<enter><enter>
>
> The "=2+3" is replaced with "5". You could also have entered
> "=x" where x is a variable.
>

Thanks..It worked :)
Reply all
Reply to author
Forward
0 new messages