# 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:

GeSHi Error: GeSHi could not find the language tex (using path /home/robfelty/robfelty.com/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

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<presentation>{\1}\r\\end{frame}/

The result is:

GeSHi Error: GeSHi could not find the language tex (using path /home/robfelty/robfelty.com/wp-content/plugins/codecolorer/lib/geshi/) (code 2)
This entry was posted in latex, linguistics and tagged , , . Bookmark the permalink.

### 3 Responses to Vim regex-fu for LaTeX

1. Jan Hlavacek says:

Instead of selecting lines, you can also do

:%g/\\subsection/s/{$$.*$$}/{\1}\r\\begin{frame}\r\\frametitle<presentation>{\1}\r\\end{frame}/

The advantage is that it will work even if you have other text between the lines with \subsection.

2. Pingback: hlavacek