hgiesel
hgiesel

Reputation: 5648

Get raw string value

In vim there is the string() function, which gets the string value of a function:

let a = {'foo':'bar'}
echo string(a)
" => '{'foo':'bar'}'

However I have a function where I cannot use this:

let obj = {'cmd':"iHello\<cr>World"}
execute 'normal! '.obj.cmd
" => works fine

execute 'nnoremap a :normal! '.obj.cmd
" => prints 'Hello' when pressing A once, 'HellHelloo\nrld' when pressing A twice. With \n I mean a literal newline.

execute 'nnoremap a :normal! '.string(obj.cmd)
" => when pressing A, wants to jump to mark H (because of 'H)

Is there transformation function to make nnoremap interpret obj.cmd correctly?

Upvotes: 1

Views: 512

Answers (1)

1983
1983

Reputation: 5963

Forget string() and objects for a moment and just concentrate on those :exes.

When you use :normal, a following <CR> will run the command and not be inserted into the buffer. So if you want to use :normal, you need to escape the <CR> with a <C-V>. Remember that mappings are executed as if you typed them.

:execute 'nnoremap a :normal! iHello<C-V><CR>world<CR>'

Notice the <CR> at the end of the string to finish the :normal command.

However, you don't need :normal here. It's not accomplishing anything, and you could do a more straightforward:

:execute 'nnoremap b iHello<CR>world'

Upvotes: 1

Related Questions