1

I use vim-LaTeX-suite as my editor, and i really like it, However i would like to know how to change the environment around the courser an example follows:

say you are in the following situation

% Preamble stuff
\begin{document}
%
\begin{itemize}
\item Who forgot the cake
    \begin{itemize}
        \item Albert
        \item Bob
        \item Robert
    \end{itemize}
\item what cakes do we like
    \begin{itemize}
         \item bananna
         \item chocklate
     \end{itemize}
\item who should bring the cake next week
     \begin{itemize}
        \item Albert
        \item Bob
        \item Robert
     \end{itemize}
\end{itemize}
%
\end{document}

and you are on the (around the middle) line \item what cakes do we like and you wan't to change the outmost itemize environment to an enumerate environment. How do you do ths?

1
+50

The script itself as included with ViM-LaTeX only looks for the innermost environment. However, you can use the following script to do the same for the outermost

function! Tex_ChangeOutestEnvironments() 

  let env_line = searchpair('\\begin{\(document\)\@!', '', '\\end{\(document\)\@!', "bnr")

  if env_line != 0
      let env_name = matchstr(getline(env_line), 'begin{\zs.\{-}\ze}')
  endif

  if !exists('env_name')
    echomsg "You are not inside environment"
    return 0
  endif

  exe 'echomsg "You are within a '.env_name.' environment."'
  let change_env = PromptForEnvironment('What do you want to change it to? ')

  if change_env == 'eqnarray'
    call <SID>Change('eqnarray', 1, '', env_name =~ '\*$')
  elseif change_env == 'align'
    call <SID>Change('align', 1, '', env_name =~ '\*$')
  elseif change_env == 'eqnarray*'
    call <SID>Change('eqnarray*', 0, '\\nonumber', 0)
  elseif change_env == 'align*'
    call <SID>Change('align*', 0, '\\nonumber', 0)
  elseif change_env == 'equation*'
    call <SID>Change('equation*', 0, '&\|\\lefteqn{\|\\nonumber\|\\\\', 0)
  elseif change_env == ''
    return 0
  else
    call <SID>Change(change_env, 0, '', '')
    return 0
  endif

endfunction 

function! <SID>Change(env, label, delete, putInNonumber)

  let start_line = line('.')
  let start_col = virtcol('.')

    let first = '\begin{' . a:env . '}'
    let second = '\end{' . a:env . '}'

    let bottom = searchpair('\\begin{\(document\)\@!','','\\end{\(document\)\@!','r')
    s/\\end{.\{-}}/\=second/
    let top = searchpair('\\begin{\(document\)\@!','','\\end{\(document\)\@!','br')
    s/\\begin{.\{-}}/\=first/

  if a:delete != ''
    exe 'silent '. top . "," . bottom . 's/' . a:delete . '//e'
  endif

  if a:putInNonumber == 1
    exe top
    call search('\\end\|\\\\')
    if line('.') != bottom
      exe '.+1,' . bottom . 's/\\\\/\\nonumber\\\\/e'
      exe (bottom-1) . 's/\s*$/  \\nonumber/'
    endif
  endif

  if a:label == 1
    exe top
    if search("\\label", "W") > bottom
      exe top
      let local_label = input('Label? ')
      if local_label != ''
    put = '\label{'.local_label.'}'
      endif
      normal $
    endif
  else
    exe 'silent '.top . ',' . bottom . ' g/\\label/delete'
  endif

  if exists('local_label') && local_label != ''
    exe start_line + 1.' | normal! '.start_col.'|'
  else
    exe start_line.' | normal! '.start_col.'|'
  endif
endfunction 

It's an edited version of the original that makes use of the 'r' flag in the searchpair function in order to look for the outermost pair, and excluding document which is also an environment.

Currently it doesn't support changing display maths environments, because they were throwing errors for me.

In order to use this function, you should save it as a file and source it at some point, and then set a mapping to use it. Perhaps

map <C-S-F5> :call Tex_ChangOutestEnvironments()<CR>

to map it to control-shift-f5.

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.