Friday, September 16, 2011

Re: Representing carriage returns in a register



On Wed, Sep 14, 2011 at 4:49 PM, Benjamin Fritz <fritzophrenic@gmail.com> wrote:
On Tue, Sep 13, 2011 at 2:06 PM, Benjamin R. Haskell <vim@benizi.com> wrote:
>
> ^M == \r CR carriage return
> ^J == \n LF line feed
>
> So, I think it's what's described at:
>
> :help CR-used-for-NL
> or
> :help sub-replace-special
>
> Maybe not the right explanation(s)... but I think it's the same reason:
>
> \n sometimes means <NUL>, sometimes <NL>
> \r sometimes means <NL>, sometimes <CR>
>

Possibly. But these help sections seem to apply to search/replace, not
register manipulation.

This should work, in my opinion:

:let @a='<C-R><C-R>a'

It doesn't. First of all, theres the ^J->^M conversion. Then, it adds
an additional ^J character at the end to make the register linewise,
even though it is already linewise.

I think something is wrong here.

I also kind of expected searching for the yanked text to work, but
that makes...slightly more sense. I found that searching with ^J
characters doesn't work either, you need to use \n (entered with
<C-V><C-J> and displayed as ^@).

I agree - it would be really useful to be able to search for the contents of a register. I've identified 3 problem characters that get in the way:

* \ must be escaped (use \\ to match a single \)
* / must be escaped when searching forwards
* ? must be escaped when searching backwards
* newline characters require special treatment (as discussed in this thread)

My Vimscript skills aren't up to much, so I posted this question on StackOverflow:


Based on Andrew Radev's suggestion, I came up with this little snippet:

cnoremap <c-x> <c-r>=<SID>PasteEscaped()<cr>
function! s:PasteEscaped()
  echo "\\".getcmdline()."\""
  let char = getchar()
  if char == "\<esc>"
    return ''
  else
    let register_content = getreg(nr2char(char))
    let escaped_register = escape(register_content, '\'.getcmdtype())
    return substitute(escaped_register, '\n', '\\n', 'g')
  endif
endfunction

Suppose that I want to search for a string that I've yanked into the 'a' register. If I do

    /<c-r>a<CR>

Then I'll run into problems if the 'a' register contains any of the characters mentioned above. This snippet of code allows me to do this instead:

    /<c-x>a<CR>

The <c-x> mapping does all of the escaping necessary to make \, /, ? and newline characters 'safe' in the context of a search prompt.

Please try it out, and let me know if you find any problems in my code.

Drew

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: