pedrosaurio
pedrosaurio

Reputation: 4926

Global substitution for latex commands in vim

I am writing a long document and I am frequently formatting some terms to italics. After some time I realized that maybe that is now what I want so I would like to remove all the latex commands that format text to italics.

Example:

\textit{Vim} is undoubtedly one of the best editors ever made. \textit{LaTeX} is an extremely powerful, intelligent typesetter. \textbd{Vim-LaTeX} aims at bringing together the best of both these worlds

How can I run a substitution command that recognizes all the instances of \textit{whatever} and changes them to just whatever without affecting different commands such as \textbd{Vim-LaTeX} in this example?

EDIT: As technically the answer that helps is the one from Igor I will mark that one as the correct one. Nevertheless, Konrad's answer should be taken into account as it shows the proper Latex strategy to follow.

Upvotes: 1

Views: 938

Answers (2)

Igor Chubin
Igor Chubin

Reputation: 64563

Use this substitution command:

% s/\\textit{\([^}]*\)}/\1/

If textit can span muptiple lines:

%! perl -e 'local $/; $_=<>; s/\\textit{([^}]*)}/$1/g; print;'

And you can do this without perl also:

%s/\\textit{\(\_.\{-}\)}/\1/g

Here:

  • \_. -- any symbol including a newline character
  • \{-} -- make * non-greedy.

Upvotes: 3

Konrad Rudolph
Konrad Rudolph

Reputation: 545618

You shouldn’t use formatting commands at all in your text.

LaTeX is built around the idea of semantic markup. So instead of saying “this text should be italic” you should mark up the text using its function. For instance:

\product{Vim} is undoubtedly one of the best editors ever made. \product{LaTeX}
is an extremely powerful, intelligent typesetter. \product{Vim-LaTeX} aims at
bringing together the best of both these worlds

… and then, in your preamble, a package, or a document class, you (re-)define a macro \product to set the formatting you want. That way, you can adapt the macro whenever you deem necessary without having to change the code.

Or, if you want to remove the formatting completely, just make the macro display its bare argument:

\newcommand*\product[1]{#1}

Upvotes: 6

Related Questions