kevin
kevin

Reputation: 785

how <c-r>= works in vim

Adding the code in vimrc causing

"E488: Trailing characters: <c-r>=12+34<cr>" 

always shows up

<c-r>=12+34<cr>

what's going on there?

my original code:

function! CleverTab()
    if strpart( getline('.'), 0, col('.')-1 ) =~ '^\s*$'
        return "\<Tab>"
    endif
    if pumvisible()
        return "\<C-N>"
    endif
    let s:codecompl = CodeComplete()
    "<c-r>=s:codecompl<cr>
    <c-r>=12+34<cr>
    "exec " " .s:codecompl. "\<enter>"."."
    "if g:swith_region_flag == 1
    "    return SwitchRegion()
    "else return s:codecompl
    return ''
endfunction

Upvotes: 0

Views: 148

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172768

The <C-R> is an insert-mode command, you cannot simply put it in a Vimscript function, which executes Ex commands. Instead you'd have to use :normal! i to issue the normal mode command i to re-enter insert mode. Use :execute to be able to use the <...> key notation, and you get:

:execute "normal! i\<c-r>=12+34\<cr>"

That said, when you're in a map-expression that returns keys (as you appear to be here), you can't use :normal (the text is locked), and you should just :return the keys:

:return "\<c-r>=12+34\<cr>"

Upvotes: 1

Related Questions