0

I've created a macro to add a piece of code.
The macro runs fine.
But when added to .vimrc, it reports error.
(The macro becomes weird when paste here.)
let @p='iprocess (clk, resetn)
€kbbegin
if (resetn = '0') then

€kbelse€kbif (clk'event and clk = '1') then

€kbend if;
€kbend if;
€ku€@7€kb€kb€kb€kd€kuprocess;q€kb'

enter image description here

enter image description here

2
  • 1
    Your problem comes from the way you entered the special characters. You can try to use this command: call setreg('p', "iprocess (clk, resetn)^M€kbbegin^Mif (resetn = '0') then^M") note that the ^M should be added by pressing <C-v><CR> in insert mode to enter the keycode of the return key.
    – statox
    Jan 26 '18 at 12:01
  • Great! Calll setreg is working.
    – Fisher
    Jan 26 '18 at 14:54
1

The way you initialize has two flaws:

First, it is usually better to use setreg() to set the value of a register, it is more robust than let @X = and allows you to choose the type of the register (characterwise, linewise, blockwise)

Second, your string contains the keycode of some key (e.g. ^M for <CR>). I guess that you recorded your macro and tried to get what you recorded and that's what caused the problem. In your vimrc you need to write the following:

call setreg('p',  "iprocess (clk, resetn)^M€kbbegin^Mif (resetn = '0') then^M")

where ^M is not entered manually but is entered by pressing Ctrl+v then Enter this allows you to write the keycode of the key which setreg() can then interpret properly. Note that you should do that with the others special keys like <Esc> for example.

Edit As mentioned by @Rich in the comments using \<CR> inside a double quoted string is arguably more robust than <C-v><CR>.

See:

2
  • 1
    Yes, macro content was pasted by <C-r><C-r>a. Normally I'll not manually edit the register content. It's great to know the details about ^M. Thanks a lot!
    – Fisher
    Jan 26 '18 at 15:16
  • Might be worth mentioning that, inside a double-quote string, \<CR> can be used instead of CTRL-V Enter? It's arguably more robust, especially when copy/pasting answers from SE sites!
    – Rich
    Jan 26 '18 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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