0

I'll try to illustrate as clear as I can.

Suppose I have the following text open in vim/nvim

 _________
|         |
|  9999   |
|         |
 ---------

If select 9999 visually and then hit r8, this will happen

 _________
|         |
|  8888   |
|         |
 ---------

Can I do the same replacing character(s) with another one that's on a register, mostly my system clipboard ? Suppose I wanted to change the vertical bars on the box's border, replacing | with and making something like

 _________
│         │
│  8888   │   <--- The bars are little longer though it may seem simliar to before
│         │
 ---------

I am aware that I can do this by selecting the box and using a substitute command, s/|/│/g but that is not what I want. Furthermore, I don't think this method would work properly on visual block mode (with a vertical selection).

So, is there a way to do this using r followed by something?

1
  • Note that the box-drawing characters have digraphs, like vv for the vertical bar. See :help digraph
    – D. Ben Knoble
    Commented Aug 15, 2023 at 16:05

4 Answers 4

1

Standard "r" command does not accept register argument. But you can write some mapping of your own if you really like so. For example,

xnoremap <expr>r v:register is '"' ? 'r' : 'r'..getreg()[0]

Then (in visual mode) if pressing "r" without register override (and also clipboard option is neither "unnamed" nor "unnamedplus") it still works as usual. But if used with non-default register, like "+r, then it just takes the first char from that register and never waits for user input.

0

If you visually select (block select Ctrlv) the first vertical bar and type:

r│

The the bar will be replaced:

 _________
│         |
│  8888   |  
│         |
 ---------

If you repeat the operation on the other bar:

 _________
│         │
│  8888   │  
│         │
 ---------
1
  • The OP wants to enter the contents of an existing register, not type it in.
    – Rich
    Commented Aug 15, 2023 at 9:26
0

A substitution would work well, see :help \%V.

As for your immediate problem, it can be solved with a bit of lateral thinking… and whatever you usually press to paste from the system clipboard (Ctrl+V, Cmd+V, etc).

Here it is Cmd+V:

r

If your Vim is built with clipboard support, then you can place what you want in register + and paste it from the system clipboard as above:

:let @+ = @a
0

In your specific example where you're only replacing a single character with the contents of a register you can use s instead of r and insert the register contents via CTRL-R:

Without making a visual selection, type sCtrl-R+Esc.

This technique won't work for replacing multiple characters with the register contents, like when you convert 9999 into 8888 earlier in your question. For that, you need Matt's answer.

Your Answer

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

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