I’ve been trying to use vim to simplify writing latex. To this end, I want to write a function to make it easy to write matrices. Here’s what I want it to do.
While in insert mode
if I type mmatrix (not a typo. I want two m’s)
I want it to ask me the number of rows and columns I need
Then open a blank matrix with the required number of placeholders (denoted <++>)
Here’s the code I wrote
imap mmatrix <C-o>:call Matrix
func! Matrix(rows, columns)
for row in a:rows
for col in a:columns
exec "normal! i<++>& "
endfor
exec "normal! i\\\\ <CR>"
endfor
endfunction
So for a 2×2 Matrix, it should look like
<++>& <++>\\
<++>& <++>\\
However, this isn’t working. May I know how to modify this file to make it do what I want it to?
I got this to work:
another option would be using a command instead of an imap, like:
then you could use
:M 2,4in normal mode to call the function.