Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am often burdened with "converting" and "including" stuff into larger documents. I have compiled some scripts and voodoo to ease my work but rather often I'm still facing the task of manually converting a list given to me as plain text into LaTeX, for example:

1. Foo
2. Bar

When doing so, I select the plain text list in visual mode and replace the prefix with \item. In this example s/^../\\item/ yields:

\item Foo
\item Bar

Then I select the list again and hit F5 and when asked which environment I want i type itemize (or whatever) and this finally leads to what I want:

\begin{itemize}
\item Foo
\item Bar
\end{itemize}

My question is: How can I create a shortcut in Vim that does both steps in one? Ultimately I just want to select a list and hit F6.

Of course I will need to redefine the function (or just global variables) ocationally to use a different pattern when replacing prefixes with \item and to use different environments. I think I can do that once I have the initial shortcut/function working.

share|improve this question
    
maybe you can use pandoc to export to latex - markdown for example uses 1. foo syntax for enumerated lists and pandoc can translate markdown to LaTeX. see johnmacfarlane.net/pandoc –  epsilonhalbe Jul 16 '12 at 20:47
add comment

1 Answer

up vote 3 down vote accepted

If your list of lines is surrounded by blank lines, the following mapping will work:

:map <f6> {jV}:s/^../\\item/<cr>{jV}k<f5>itemize<cr>

Place the cursor anywhere in your numbered list and press F6

Update

A more general approach is to search backwards until the first line which does not start with a digit, and select from that until the last line which starts with digit. In the selected range, the digits are replaced by \item. Then we have to mark again the same range to comvert it into an itemize, and this is tricky because we cannot use the same regexp to select (because the digits were replaced). We can use vi "marks" to mark the points when we find them, and use those marks to select again the same set of lines. The following mapping does all this (and uses mark names a and b):

:map <f6> ?^\D<cr>jmaV/<cr>kmb:s/^../\\item/<cr>'aV'b<f5>itemize<cr>

Perhaps it can be illustrative to dissect that black magic:

  • :map <f6> creates a mapping for key F6. When that key is pressed, the following sequence of keystrokes will be "executed".
  • ?^\D<cr> Moves the cursor to the first line (backwards) which matches the regexp ^\D, which means "beginning of line followed by a non-digit"
  • j Advances to the next line
  • ma stores the current cursor position in mark a
  • V starts selection in visual mode
  • /<cr> advances to the next line (forward) which matches the last used regexp, i.e. to the first line which starts with a non-digit char.
  • k moves to previous line
  • mb stores the cursor position in mark b
  • :s/^../\\item/<cr> replaces the two first chars of each line in the selected range by \item
  • 'a' moves to the stored marka` (beginning of the selection)
  • V starts visual selection again
  • 'b' moves to markb. At this point we have selected again the set of\item`s
  • <f5>itemize<cr> presses key F5 and selects itemize environment. This calls a latex-suite macro which encloses the selection in an itemize environment.

This cover the cases in which there are blank lines between items, and non-blank lines surrounding them.

share|improve this answer
    
There are not always empty lines before and after, but sometimes there are empty lines in between. Your solution covers about 60% of all use cases ... I think I need to select the entire block to tell vim which part to itemize. –  user1129682 Jul 16 '12 at 15:28
    
Your updated approach doesn't work. It's functionallity is equivalent to just pressing F5. But I got everything I could ask for out of your post. Thank's a lot! –  user1129682 Jul 20 '12 at 13:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.