Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vimrc write to file

Tags:

vim

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?

like image 794
WiFO215 Avatar asked Feb 14 '26 17:02

WiFO215


1 Answers

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.

like image 134
Conner Avatar answered Feb 17 '26 18:02

Conner