Search code examples
linuxunixvimpasteex

Multiline paste (ctrl-r) into ex or search|replace ^M with \r


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.


Solution

  • 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.