You asked for an explanation. Let's chop your command up and look at it bit by bit:
%s/value_one/\=input('replace <'.submatch(0).'> with: ')/g
% <- in the whole buffer
s <- substitute (see :help :substitute)
/value_one <- the search pattern "value one"
/\= <- with the result of an expression (see :help :s\=)
input <- prompt the user for input (see :help input())
('replace <'.submatch(0).'> with: ') <- this is what the user sees.
submatch(0) is whatever was matched ("value_one" in our case) and the dots concatenate the strings.
/g <- do this multiple times per line (see :h :s_g)
And it does exactly what one would expect: matches every "value_one" and prompts with what to replace. Every time.
It's not obvious why you would need this. If you simply want to "replace all", the plain :substitute
command as mentioned in Aviik's answer is the way to go. It's actually easier to write :%s/value_one/new_value/g
than to prompt for user input.
If you want to replace "value_one" with something several times on a line, the way to go is to do substitution on the current line only, i.e. simply by leaving out the %
range:
:s/value_one/replacement_everywhere_on_this_line/g
The proper use of ranges is another key to mastering :substitute
(and many other commands that use ranges). You can choose to operate on the whole buffer, the current line, a visual selection or arbitrary lines given as absolute or relative line numbers etc. See :help :range
.
Coincidentally, the up arrow is working in the command line, so you can quickly access the last command and change it.
What's more, Vim will remember the last search pattern. To replace "value_one" with "foo" on one line, but with "bar" on another line, you can use the following pair of commands:
:s/value_one/foo/
:s//bar/
It is also possible to write a function that queries the user for a replacement string and then constructs a :substitute
command from it. There's just no point in doing so. That would re-invent behavior already in the editor.
I suspect the question comes from a misunderstanding how replacing text is commonly done in Vim. Most editors have these popup windows to "search and replace (all)". Vim does not have this and you don't need it. In Vim, you specify a :substitute
command that defines the replacement. It's precise and powerful - it does not have a pretty graphical user interface.