Indexing a Latex document, I added a lot of \index[]{} commands in \footcite prenotes. It's stupid! Anyway, in my code I have a lot of :

\footcite[blabla blabla\index[n]{something} blabla.][postnote]{Reference}

or sometimes :

\footcite[blabla blabla\index[n]{something} blabla\index[n]{otherthing} blabla.][postnote]{Reference}

I would like to find some trick to suppress all the \index[n]{something} or \index[n]{otherthing} stuffs only if they are between the square brackets of a footcite prenote. Is there an easy trick to do that ?

2 Answers 2


If the \footcite is always one line, you could do something like this:

  • %g/\footcite[/s/\index.\{-}[}]//cg Select every line with "footcite" on it, and only on those lines take every string of the form "\index any characters ]" and remove it.

The c ensures that if some of the indexs are outside of the cite and you want to keep those, you can just skip replacing those.

  • Arrg! Most of time it is a multiple line prenote. But maybe your tip could help a bit.
    – Texxet
    Sep 10, 2015 at 20:39
  • It solved my problem
    – Texxet
    Sep 10, 2015 at 21:46

Substitute and nested substitute

Another way, which handles the command being split over multiple lines, is


You would have to run this more than once if you want to remove more than one \index command within the same \footcite[]. I'm actually not sure in this case how match one thing (\footcite[]) but make many substitutions. Normally you can use use a replacement expression and call a 'nested substitute' (s/pattern/\=substitute()/, see :help sub-replace-expression), but I couldn't figure out how to match the whole \footcite[] string properly. So, that is left as a lesson for the reader (and the writer, for another day), or for knowledgeable vimmers to answer in the comments.


Instead you can do it with a global/normal/visual/substitute command:

g/\\footcite/normal /[^Mvi]:s/\\index\[[^\]]*\]{\([^}]*\)}/\1/g^M

nb the ^M is Ctrl-V+<CR>

This command

  1. finds the right lines with global
  2. issues a normal command to
  3. searches forward to [
  4. visually selects inside []
  5. substitutes over visual selection

This looks more complicated than it is because (as I learned here) you can execute a recorded macro from a global command. So: move cursor to a \footcite command and record while you make a manual substitution


  • qq record macro to q register
  • f[ forward to [ -- or use /[<CR> if [ can be on the next line
  • vi] visually select inside []
  • :s/... do the substitution

With the macro recorded you can do

:g/\\footcite/normal! @q
  • You could probably fit a \=substitute() inside the replacement part, similar to vi.stackexchange.com/a/3927/1800 (minus the looping part; to remove all the \index commands at once).
    – VanLaser
    Sep 10, 2015 at 22:28
  • @VanLaser Yes! Or like vi.stackexchange.com/questions/4432/…, but like I said, this writer needs to see a sunset and a sunrise first :)
    – jjaderberg
    Sep 10, 2015 at 23:00
  • :s/^You/One ^ - Roger that & Good night! :)
    – VanLaser
    Sep 10, 2015 at 23:21
  • @VanLaser It was, thank you :) I was not able to use \=substitute() because I couldn't figure out how to match and pass the entire \footcite[] to substitute() when it contains nested [], that is, how to match to the correct ]. This seems like it would be a common problem, but I couldn't figure it out. Syntax files solve it for {} by making many overlapping matches, but do you know how to do it in a single match?
    – jjaderberg
    Sep 11, 2015 at 7:54
  • :normal f[v% could probably help to select a good range, if cursor is on a footcite. Then something like :'<,'>s/\%Vindex ... to select the exact range (lines & columns) for substitution.
    – VanLaser
    Sep 11, 2015 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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