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 2x2 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:
func! Matrix(rows, columns)
for row in range(a:rows)
for col in range(a:columns)
exe "norm i<++>& "
endfor
exe "norm Xi\\\\\\\<cr>"
endfor
endfunction
another option would be using a command instead of an imap, like:
command! -nargs=1 M :call Matrix(<args>)
then you could use :M 2,4 in normal mode to call the function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With