Sublime Text + LaTeXTools: How To Automatically Delete Generated Files After Compilation

latexpdflatexsublime-text-3

I use Sublime Text Editor 3 with LaTeXTools to write in LaTeX.

I compile files by pressing Control+B. If I name my text file as main.tex, the generated files will be:

 - main.aux
 - main.bbl
 - main.blg
 - main.log
 - main.pdf
 - main.synctex.gz

Is there a way to edit the LaTeXTools package so that, after the compilation process is over, the .aux, .bbl, .blg, .log, and .synctex.gz files are deleted?

Best Answer

Method 1:

Install the Sublime Text LaTeXTools package.

By default the Command Pallet -> LaTeXTools: Delete temporary tiles is mapped to Ctrl-L, backspace by default:

// super+l,backspace to remove temporary files
{ "keys": ["super+l", "backspace"],
  "context":  [{"key": "selector", "operator": "equal", 
                  "operand": "text.tex.latex"}],
  "command": "delete_temp_files"},

The LaTeXTools package is also documented here.

To adjust what LaTeXTools cleans up without worrying about package updates, choose Preferences -> Package Settings -> LaTeXTools -> Settings -- User and adjust the following block of code:

// ------------------------------------------------------------------
// Temporary file settings
// ------------------------------------------------------------------
// Ends of the names of temporary files to be deleted
"temp_files_exts": [
    ".blg",".bbl",".aux",".log",".brf",".nlo",".out",".dvi",".ps",
    ".lof",".toc",".fls",".fdb_latexmk",".pdfsync",".synctex.gz",
    ".ind",".ilg",".idx"
],
// Folders that are not traversed when deleting temp files
"temp_files_ignored_folders": [
    ".git", ".svn", ".hg"
],

To tie both ^b and ^l together: use the Chain of Command Package as described by https://stackoverflow.com/a/27994582, and modify the build keyboard shortcut to include delete_temp_files.

Method 2:

Install latexmk. You will want this anyway, as it runs LaTeX the required number of times, and runs biber/BibTeX as needed until it builds the pdf correctly.

Then within Sublime Text Under Tools->Build System choose New Build System and enter the following:

{
    "shell_cmd": "latexmk -pdf \"$file\" && latexmk -c",
    "selector": "text.tex.latex"
}

If latexmk isn't in your default path, you may need to specify the full path. The above will work on OSX and Linux. The && is a bash script directive that tell the shell to run the second command if the first command was successful. The -c runs the cleanup. Which files are cleaned up can be adjusted through the configuration file for latexmk.

Related Question