I have a function (from here) that requires an argument enclosed in quotes.

if !exists('*Preserve')
    function! Preserve(command)
            " Preparation: save last search, and cursor position.
            let l:win_view = winsaveview()
            let l:old_query = getreg('/')
            silent! execute 'keepjumps' . a:command
            " try restore / reg and cursor position
            call winrestview(l:win_view)
            call setreg('/', l:old_query)

I want to put this in a macro. I have tried,

let @d=':call Preserve(":normal ?^# ^Mjo## DESCRIPTION^]")'


let @d=':call Preserve('':normal ?^# ^Mjo## DESCRIPTION^]'')'

and various other ways, but nothing works.

How do I create a macro that calls this function without errors?


Among the answer was the suggestion that I simply record the function. That's good. However, I keep a list of frequently-used macros in the form:

let @d='<contents of macro>'

The reason is, some of the macros are very long, and occasionally I lose them from the registers and need to re-load them so-to-speak. Having to manually record the macro again would be a PITA. (BTW I'm aware I could perhaps record the function call and then append it (eg. qD) to a macro created with :let @d='[...]; but this is far from optimal.)

So, is it at all possible to :call a function (as above) without running into errors with quoting?

  • you are at least missing an enter after calling the function. I would rather suggest, to record the macro and manually call the function, which will make sure the register is set correctly to call the function next time it is executed. Jun 29 at 6:11
  • as said before, do not forget the enter after :call, otherwise Vim would not execute the function Jun 29 at 7:49
  • Perhaps instead of a macro a command would suit you better ? They won’t get lost, and they have better names
    – D. Ben Knoble
    Jun 29 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.