tags:

views:

192

answers:

5

I am editing a LaTeX file with vim. When I am in the \begin{itemize} environment, is there any way to tell vim to autoinsert \item whenever I open a new line?

+1  A: 

I know noting about latex but I think it is a good idea to search in vim scripts

use the search button up left :D

for example search for

latex auto completion

ali
A: 

I would recommend http://vim-latex.sourceforge.net. This package defines several maps useful for latex. In particular for inserting \item you press <ATL-I>

skeept
Thanks, this worked great. Although I may be pushing the envelope a little bit, is there any way where `\item` is added whenever a new line is opened automatically without having to rely on alt-I?
Samad Lotia
In vim-latex, you can say `EIT` to start an `itemize` environment. vim-latex provides other shortcuts too.
Alok
+1  A: 

I can hit Cntl-I and it'll put it in for me in either normal mode or insert mode. This is what I put in my .vimrc:

:imap <C-i> \item 
:nmap <C-i> o\item 

Note that there is a space at the end of \item.

Samad Lotia
+1  A: 
function CR()
    if searchpair('\\begin{itemize}', '', '\\end{itemize}', '')
        return "\r\\item"
    endif
    return "\r"
endfunction
inoremap <expr><buffer> <CR> CR()

Put this into your .vim/ftplugins/tex.vim file (or any .vim inside .vim/ftplugins/tex directory).

ZyX
This worked great, thanks for this suggestion!
Samad Lotia
A: 

I hacked the script ZyX supplied and came up with this. It adds support for the o and O commands. It does not require LaTeX-VIM.

function AddItem()
  if searchpair('\\begin{itemize}', '', '\\end{itemize}', '')
    return "\\item "
  else
    return ""
  endif
endfunction

inoremap <expr><buffer> <CR> "\r".AddItem()
nnoremap <expr><buffer> o "o".AddItem()
nnoremap <expr><buffer> O "O".AddItem()
Samad Lotia