1

After doing

:%s/foo/bar/g

I can type

:<C-r>/

to place foo into the command line.

How can I place bar into the command line?

I ultimately want to do a series of:

:%s/foo/bar/g
:%s/bar/baz/g
:%s/baz/qux/g

etc., inspecting the resulting source file's outputs in-between (and don't want to rely on replace-undo, <C-r>/ because I might edit the file manually in-between too)

2 Answers 2

2

You can search for the last used substitute string with /~/. This should work as long as the substitute string does not contain any specials like \0...\9, \U, \L, etc.

0

<C-r>/ inserts a special register "/ (the last search string) at the actual position in the command line. Type :register and you'll see all registers with their content. There is no register for the subtitute-text.

Register are AFAIK the only way to keep such informations.

You could write a vim function, that extracts the replacement part of the substition command stored in register ": with a regular expression. You could bind that function to a key. But that only works as long as you do not enter another command, which would overwrite the ": register.

Another idea: You have to type foo, bar and baz at least once, you could instead search for them in the text, highlight them with v and yank them into a separate register:

\foo<CR>vll"ay
\bar<CR>vll"by
\baz<CR>vll"cy

Now you can use the registers as long as you want (as you don't overwrite them).

:%s/<C-r>a/<C-r>b/g
:%s/<C-r>b/<C-r>c/g
:%s/<C-r>c/qux/g
3
  • Thanks for the final alternative suggestion. Unfortunately this one won't work for me because b depends on my interpreted results of a, c on b etc.—I can't know them ahead of time. I guess your vim function idea is a good one, and thanks for introducing me to the ": register. I could first check if the contents are indeed a substitution, then rewrite my special register.
    – user9024
    Commented Jan 26, 2021 at 13:18
  • But since it's not built into VIM, it seems likely there would exist plugins for it...
    – user9024
    Commented Jan 26, 2021 at 13:19
  • @theonlygusti There are some interesting search and replace plugins. Perhaps you find something usefull? If you plan the compute bar from foo e.g, I'd like to point you to the expression register "= which could be helpful as it executes a random command that you store in it and prints it to the target location. brianstorti.com/vim-registers (The expression and the search registers) Commented Jan 26, 2021 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.