latex, linguistics

Vim regex-fu for LaTeX

When writing a beamer presentation with LaTeX, I organize my presentation into sections and subsections. Frequently, the title of the first frame (slide) in a subsection has the same name as the subsection. Let’s say I start off with the following structure:

\section[corpora]{Accessing text corpora}
\subsection[gutenberg]{The Gutenberg Corpus}
\subsection[chat]{The web and chat Corpus}
\subsection[brown]{The Brown Corpus}
\subsection[reuters]{The Reuters Corpus}
\subsection[inaugural]{The Inaugural address Corpus}
\subsection[annotated]{Annotated corpora}
\subsection[foreign]{Corpora in other languages}
\subsection[DIY]{Loading your own corpora}

For each subsection, I want to put in one frame, with the name of the subsection being the name of the frame. Regular expressions to the rescue! In vim, all I have to is use V to select each line with subsection, then I hit :, which allows me to operate on those lines only. '<,'> is automatically inserted after the colon, which stands for “from the beginning of the highlighted section to the end of it”. Then I use s to perform my substitution. \r inserts a new line.

:'<,'>s/{\(.*\)}/{\1}\r\\begin{frame}\r\\frametitle{\1}\r\\end{frame}/

The result is:

\section[corpora]{Accessing text corpora}
\begin{frame}
\frametitle{Accessing text corpora}
\end{frame}
\subsection[gutenberg]{The Gutenberg Corpus}
\begin{frame}
\frametitle{The Gutenberg Corpus}
\end{frame}
\subsection[chat]{The web and chat Corpus}
\begin{frame}
\frametitle{The web and chat Corpus}
\end{frame}
\subsection[brown]{The Brown Corpus}
\begin{frame}
\frametitle{The Brown Corpus}
\end{frame}
\subsection[reuters]{The Reuters Corpus}
\begin{frame}
\frametitle{The Reuters Corpus}
\end{frame}
\subsection[inaugural]{The Inaugural address Corpus}
\begin{frame}
\frametitle{The Inaugural address Corpus}
\end{frame}
\subsection[annotated]{Annotated corpora}
\begin{frame}
\frametitle{Annotated corpora}
\end{frame}
\subsection[foreign]{Corpora in other languages}
\begin{frame}
\frametitle{Corpora in other languages}
\end{frame}
\subsection[DIY]{Loading your own corpora}
\begin{frame}
\frametitle{Loading your own corpora}
\end{frame}