1

For example, substitute : with register 0's content.
What's the correct command for :s/:/"0/.

2

N.B. In all the below, I presume that your question is only about the method of accessing the register's contents from an ex command, and that the rest of your :substitute command (which replaces only the first match on the current line) does what you require. If you actually want to replace globally, you will need to use a % range and the g flag: :%s/find/replace/g

Interactively in Command Line Mode

If this is being done interactively, you can do so by inserting the contents of the register into the command using Ctrl-R:

Type the first part of your command, :s/:/

Then type Ctrl-R0 to insert the contents of the yank register "0 into the command line.

Note that you may need to then edit the command line slightly before pressing Return to run the substitution. e.g. if the register contained a string that included a forward slash /.

In Vimscript

If this is in a script, then you can use the @0 notation or the getreg() function to access register contents. You can also then perform escaping automatically.

e.g.

execute 's/:/' . escape(getreg('0'), '/')

Update: In a Macro

If you want to do this in a macro recorded from keystrokes, the first method works fine: Ctrl-R is recorded just like other keystrokes.

If you're instead creating a macro by assigning a string value to a register, you can use something like the below:

:let @q = ":s/:/\<C-R>0\<CR>"

I'm using the :let command above for simplicity, but the setreg() function is more robust.

Further Reading

  • :help c_CTRL-R
  • :help expr9
  • :help getreg()
  • :help :execute
  • :help string
  • I wish to use it in a macro. Could you point me the command. – Fisher Jan 30 '18 at 13:53
  • @Fisher See my update – Rich Jan 30 '18 at 14:36
1

Besides @Rich's solution of using a substitution, :s, uou can use the c, gn, and <c-r> to do this as well.

cgn<c-r>0<esc>

Now you can simply spam repeat, . to do the next substitution.

However if spamming the dot command is not desirable, then you can make a macro to do it for you.

:let @q = '.@q'

Now you can simply run the macro from the "q register via @q which will repeat the . command until it runs out of matches.

Related Vimcasts episode: Operating on search matches using gn.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.