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

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

| improve this answer | |
  • Removing a: did work but also \begin... and \end{... are wrong. It has to be \\begin... and \\end... – Luis Victoria Jun 9 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
| improve this answer | |

Your Answer

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

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