I'm using vim to edit my latex files and I'm trying to automate some aspects of it.

I want a command that inserts the following:



Where env is an user input. So I came up whit this

function Begin()
   call inputsave()
   let env = input("env = ")
   call inputrestore()
   execute "normal! i\begin{" . a:env . "}\n\n\end{" . a:env . "}<esc>k"

The last part , <esc>k, is meant to put the cursor above the last line.

But unfortunaly I can make it work, it tells me E121 a:env not defined. I create a custom command to call the function

nnoremap ,b :call Begin()<CR>

What am I missing?


2 Answers 2


a: is for arguments. Don't use any prefix for local variables.

PS: you'll have a better experience with a single new line and O instead of k

  • Removing a: did work but also \begin... and \end{... are wrong. It has to be \\begin... and \\end... Jun 9, 2020 at 15:11

There were two problems:

  1. Don't usea: for any local variables, as Luc Hermitte pointed out.
  2. When using the command execute you should use |expr-string| and to insert \ it should be \\.

And so, the right way of writing it is like this

function! Begin()
   call inputsave()
   let env = input("env: ")
   call inputrestore()
   execute "normal! i\\begin{".env."}\n\n\\end{".env."}\<ESC>o

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.