1

I am trying execute a nested execute command within a vimscript. I know that this command works in ex mode:

g/\(^\n\|\%1l\).\_.\{-}\n$/execute "normal! vap:call MCformat()\<cr>"

I want to be able to run that command from within a script. I have tried a number of permutations of the following code but can't get it to work.

function! RunMCformat()   
  silent! execute "normal! g/\(^\n\|\%1l\).\_.\{-}\n$/execute \"normal! vap:call MCformat\(\)\<cr>\""

endfunction

Probably I am not escaping the string properly but I don't know where I am going wrong.

1 Answer 1

3

Because of the double quotes, you'd have to escape (i.e. double) the backslashes inside the /.../ pattern definition. However, the biggest problem is the first :normal!; :g[lobal] is an Ex command. So, you're lucky, you can just prepend :silent! (which invokes Ex commands like :global), are you should be done; no nested :execute is necessary:

function! RunMCformat()   
    silent! global/\(^\n\|\%1l\).\_.\{-}\n$/execute "normal! vap:call MCformat()\<cr>"
endfunction

In general, I would avoid nesting of :execute; that's not readable in any case. Rather, extract parts of your code into a function / custom command (in which you can use :execute), and invoke that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.