Edan Maor
Edan Maor

Reputation: 10052

Vimscript - run normal! qm after running normal! @m

So, this is a weird situation I've run into while trying to write a plugin. It looks like a bug in Vim to me, but I might be missing something, so I've turned to the wisdom of StackOverflow.

Take this function:

fun! Test()
    normal! q ; Stop recording a macro.
    let @r='kkkkkkkkkkkkk'
    normal! @r
    normal! qm
endf

Basically, I stop recording a macro, try and run the contents of a register, then start re-recording a macro.

I call this function by hitting qm, doing some commands, then calling the function.

What I expect

I expect the "kkkkkk" to be execute, and then recording to start again.

What Happens

The "kkkk" is run, but recording is not started again.

I don't understand why!

Workaround

The following function does what I want, but it's a hack. I'm trying to understand the root cause of my problem:

fun! Test() normal! q let @r='kkkkkkkkkkkkk' execute "normal! " . @r normal! qm endf

I just replaced the running of the register with an execute call that directly executes the contents of the register. I tricked vim into not thinking it is running a register, basically.

Note: Why I want this

It's for a plugin. This function is simply the smallest I could make that exhibits a problem. Not worth getting into why I want this - the basic functionality I needed was to run a register from within a function, then start recording it again. In the actual use case, the register I run and record to is the same, but it didn't seem to affect this issue.

So, any ideas?

Upvotes: 2

Views: 191

Answers (1)

ZyX
ZyX

Reputation: 53604

Why can’t you just embed the function call inside a mapping:

function Test()
    let @r='kkkkkkkkkkkk'
    normal! @r
endfunction
nnoremap Q q:call Test()<CR>qm

? If “m” is actually a non-constant value then you have to use <expr>:

nnoremap <expr> Q "q:call Test()\nq".g:plugin_register

. You can, of course, use a function call inside an <expr> mapping (thus moving normal! @r to the outputs of function: return '@r' because expression is not allowed to do normal!, buffer editing, switching and moving the cursor around).

Upvotes: 3

Related Questions