Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In vim, how can I add a carriage return to a register using setreg?

I have this command in my .vimrc:

vip:normal @g<CR>

When I set the register 'g' by typing in the buffer, like this, it works:

qg<CR>jq

If I type :registers, it shows:

--- Registers ---

"g   ^Mj

After that, typing @g results in a carriage return and then the cursor moves to the next line. The ^M appears in a special color.

However, when I use the setreg command in my vimrc, if I type @g, nothing happens.

call setreg('g','^Mj')

If I type :registers, it shows:

--- Registers ---

"g   ^Mj

The ^M is not in a special color.

I have the following in my .vimrc:

map <CR> :call MyFunction<CR>

The carriage return I want to store in the register is to run MyFunction. MyFunction is called perfectly as long as I fill the buffer manually rather than using setreg.

Where have I gone wrong? My platform is Linux.

like image 991
renick Avatar asked Dec 07 '11 13:12

renick


1 Answers

You are looking for "\<cr>" or "\r"

call setreg('g',"\<cr>j")
call setreg('g',"\rj")

or more simply

let @g = "\<cr>j"
let @g = "\rj"

For more help

:h expr-quote
:h let-@
like image 194
Peter Rincker Avatar answered Sep 21 '22 20:09

Peter Rincker