1

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?

1

2 Answers 2

1

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

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

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

Your Answer

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

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