" Version 0.2 " Usage: ":TeXtags foo" will generate a tags file from foo.aux . If you " are currently editing foo.tex or foo.bar then ":TeXtags" alone will do. " The file you are editing must be in the same directory as the .aux file. " " If your LaTeX source is split among several files, the script works well " with \include{file} commands, but NOT with \input file commands. " " Using tags: If many of your labels look like \label{eqn:foo} then try " :tag eqn:foo " :tag eqn: " :tag /eqn " :ts " for example. Also try :ptag variants. For details, read " :help tagsearch.txt " If you want to use to jump to tags, either use the Visual-mode variant " or make sure that your labels are recognized as words. For example, with " the format \label{eqn:foo}, make sure that ":" is included in 'isk'. " :help ctrl-] " :help word command! -nargs=? TeXtags call TeXtags() " Generate a tags file from a LaTeX .aux file. " This version clobbers any existing tags file! fun! TeXtags(...) if a:0 let fname = a:1 " Get file name from argument list. else let fname = expand("%:t:r") " Get file name from current file. endif if !filereadable(fname . ".aux") echohl WarningMsg execute "echo 'TeXtags: could not find file " fname . ".aux'" echohl NONE return -1 endif split %:p:h/tags %d " Clobber the old tags. " put='Tags file generated by TeXtags() Vim script' put='\@input{.aux}' " dummy line, so search cannot fail 1d " Remove the blank line added by :put command. while strlen(fname) if filereadable(fname . ".aux") execute "$-1r" fname . ".aux" " Remove all lines from fname.aux except \newlabel and \@input ones: '[,'] v/^\\newlabel{.*}\|^\\@input{\f*.aux}/d " v/^\\newlabel\|^\\@input{\f*.aux}\|^\\bibcite/d " The next few lines transform \label{foo} into the tag line " foo{Tab}fname.tex{Tab}/\\label{\s\*foo\s\*}/ " Note that we use '\*' because tag searches are done with 'nomagic'. let tagline = '\1\t' . fname . '.tex\t' let tagline = tagline . '\/\\\\label{\\s\\*\1\\s\\*}\/' execute '%s/^\\newlabel{\(.\{-}\)}.*/' . tagline endif " Find the next included .aux file. Note that fname = '' if we are on " the dummy line (last line of the file). 0/^\\@input{\f*.aux} let fname = substitute(getline('.'), '.*{\(\f*\).aux}.*', '\1', '') d " Remove the \@input{} line. endwhile 0put='!_TAG_FILE_SORTED 0 /some command/' wq endfun