Let's say I have string like this:

let a = 'iHello<cr>World'

Now I can use it for a remap:

execute 'nnoremap a '.a

But in order to use it with normal!, I would have to convert <cr> to a literal newline. I know I can do this:

let b = escape(a, '<')

Now b is 'iHello\World'. But how can I parse it to become 'iHello\nWorld' (where \n is a literal linefeed character)


Instead of <cr>, use ^M in your string:

let a = 'iHello^MWorld'

You can insert this with <C-v>+<CR>, this is a graphical representation of a newline in vim.

It will work in those cases:

:execute "normal ".a
:execute "normal! ".a
:execute "nnoremap a ".a     (then press a in normal mode)

Now if your want to make it work in the following case:

:execute "nnoremap a :normal! ".a     (then press a in normal mode)

You need to use the following (c.f. this answer):

let a = 'iHello<C-v><CR>World<CR>'
  • 1
    You got to it before me. :P
    – Tumbler41
    Jun 3 '16 at 14:46
  • Well, it won't work with :normal since the ^M will execute it. You need ^V^M in the string to use it with :normal.
    – Antony
    Jun 3 '16 at 14:47
  • How so? I made it work... Can you add some details please?
    – nobe4
    Jun 3 '16 at 14:48
  • 1
    @FizzyTea: my bad, you're right, I didn't see the second example, let me update that. Thanks for the detail :)
    – nobe4
    Jun 3 '16 at 14:53
  • 1
    I was really confused about ^M, but then I noticed, that ^M is actually the graphical representation of a carriage return in vim, not necessarily a newline, maybe you should edit that.
    – hgiesel
    Jun 3 '16 at 14:56

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.