0

When I'm yanking few lines and pasting it to command-line every new line is ^M instead \r. For example if I copy next two lines in visual mode (with command Vjy):

line1
line2

and paste it in command-line (search) mode with command /<c-r>" I get:

/line1^Mline2

But I expected: /line1\rline2

What i want to do is to highlight pasted string what is helpful when I'm pasting block of code. I know i can select it with

`[v`] 

but i want to only highlight it, and anyway it can by useful to by able to paste multiline code into ex for substitute or other funny things.

builder-7000
  • 7,131
  • 3
  • 19
  • 43
undg
  • 787
  • 4
  • 14

2 Answers2

2

Copying the next paragraph in visual mode:

line1
line2
line3

and pasting it in command-line (with <c-r>") should give line1^Mline2^Mline3^M. If you want this text to be line1\rline2\rline3\r you could define the following function and map:

function! Substitute()                                                          
    silent! let g:p=substitute(@", "\\n", "\\\\r", "g")                         
    call feedkeys(":", 'n')                                                     
endfunction                                                                     
nnoremap <silent> : :call Substitute()<cr>                                      

The command let g:p=substitute(@", "\\n", "\\\\r", "g") will find every ^M in the unnamed register (:help quotequote), replace it with \r, and store output string in p. To paste the contents of p in command-line use <c-r>=p.

builder-7000
  • 7,131
  • 3
  • 19
  • 43
1

I needed to "cast" register contents into a certain (characterwise / linewise / blockwise) mode so often, I wrote the UnconditionalPaste plugin for it. It provides gcp, glp, etc. alternatives to the built-in paste commands that force a certain mode; some variants are also available as command-line mode mappings.

The <C-r><C-q> mapping queries for a separator string, then inserts the contents of a register characterwise, with each line delimited by it. So, to achieve your desired result, that would be <C-r><C-q>"\\r<CR>.

Note: If you want to search fo the multiline text, you'd actually have to use \n instead of \r.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324