Stanimirovv
Stanimirovv

Reputation: 3172

vim script exec pastes unformated text

In all honesty the title is bad. Consider the following 5 lines:

function Example()
    let @@ =  "-_-"
    execute  "normal! ]P"
    call cursor(line('.'), col('.')-1) 
endfunction

When this function is called, I expect to get -_- as output and the cursor should be moved to the left, meaning that it is at the third character, so if I press a key, like I for example I will get -_i-

What happens in reality is quite different (and to some degree interesting) The output the first time this is called is - _- and after that it's _--

I assume that "cursor" shifts the position of the word under the cursor. Basically: Why is it happening? How can I get the desired effect ?

Very important edit: Apperantly the problem isn't in the plugins. When I go for:

call Example()

It works flawlessly. Thing is it is supposed to be triggered by a key. I have currently bound it like so:

inoremap ' <C-O>: call Example()<CR> 

So now I am thinking that something in the mapping is broken...

Upvotes: 1

Views: 72

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172510

  • I cannot reproduce your strange behavior. I get ----_-_-_-_- on repeated invocations, as expected. I again suspect there are plugins at work. Try with vim -N -u NONE. As this is a paste, there's little that could influence this function, though. You could try to workaround via :noautocmd call Example(), but I'd rather try to find the root cause of this disconcerting strangeness.
  • The "-_-" is not a full line, so the ]P (paste with adapted indent) has no effect here. You could just as well have used P.
  • To move the cursor one left, rather use :normal! h. The subtraction from col('.') again only works for single-byte ASCII characters.

Upvotes: 1

Related Questions