# using input() for custom command

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:

\begin{env}

\end{env}


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"
endfunction


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?

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 '20 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
endfunction