Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a shortcut for user's build system in Sublime Text?

I've just created a build system named XeLaTeX by creating a file named XeLaTeX.sublime-build in the User directory of Sublime Text, whose content is:

{
    "cmd": ["xelatex.exe","-synctex=1","-interaction=nonstopmode","$file_base_name"]
}

What should I do, if I want to bind my F1 key to this specific build system?

Note: Ctrl + B, the default build system should not be influenced. That is to say, I could use Ctrl + B to use the default one, and the key F1, the new system, is also available at the same time.


Maybe there is another way to achieve this. Add the following text to Default(Windows).sublime-keymap will execute the command:

{"keys": ["f1"], "command": "exec", "args": {"cmd": ["xelatex.exe","-synctex=1","-interaction=nonstopmode","$file_base_name"]}},

However, $file_base_name is not defined here. Is there any method to pass current file (base_)name to exec?

like image 249
Ch'en Meng Avatar asked Mar 14 '14 10:03

Ch'en Meng


People also ask

How do I create a shortcut in Sublime Text?

Select the Key Bindings - User item under Sublime's Preferences, then add the following example line: {"keys": ["ctrl+shift+c"], "command": "insert_snippet", "args": {"contents": "hello!"}} This will add a CTRL + SHIFT + C shortcut to insert the hello!

How do I create a Build System in Sublime Text?

Sublime Text is able to run build programs such as 'make', either when a key in pressed (F7 by default), or when a file is saved. The build system to use can be select from the Tools/Build System menu. If a project is open, the selected build system will be remembered for the project.

How do I change the Build System in Sublime Text 3?

Select it, hit Ctrl B to build, and then hit Ctrl Shift B to run the resulting program. Or you can use a Build and Run option and call it by hitting Ctrl B , then selecting that option.

Where Are Sublime Text build systems stored?

Sublime populates its Tools/Build System menu based on the “. sublime-build” files stored in the Sublime “Packages” directory. Should one need to locate this, it can be found in “~/Library/Application Support/Sublime Text 2/Packages/User” (if using OS X) or the corresponding Packages/User directory on other platforms.


2 Answers

I nailed it by myself.

AFAIK, there is no such a way to pass current file name through key binding, and it's not possible to use key binding to specify a certain build system. Thus, writing a Python script is of necessity.

There are only three steps.

1. Save the following content to /Data/Package/User/compile_with_xelatex.py:

import sublime, sublime_plugin

class CompileWithXelatexCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      self.view.window().run_command('exec', {'cmd': ["xelatex.exe","-synctex=1","-interaction=nonstopmode", self.view.file_name()[:-4]]})

2. Add a line to /Data/Packages/User/Default(<your-plat>).sublime-keymap

{"keys": ["f1"], "command": "compile_with_xelatex"},

3. Open your LaTeX source file with Sublime Text, and then press F1 to compile it with XeLaTeX.

Indeed, it's a little tricky, but it works like a charm for me.

like image 91
Ch'en Meng Avatar answered Sep 28 '22 06:09

Ch'en Meng


Build systems work by either selecting them specifically in the Tools -> Build System menu, or by using a selector to match a specific syntax. If you want to use a selector, add the following line to your XeLaTeX.sublime-build file (make sure to add a comma , after the first line, the file needs to be valid JSON):

"selector": "text.tex.latex"

The build command is already bound to CtrlB and F7, but if you also want it bound to F1, open Preferences -> Key Bindings-User and add the following line if you already have custom key bindings:

{ "keys": ["f1"], "command": "build" }

If the file is empty, just add opening and closing square brackets [ ] at the beginning and end of the file, respectively, as it also needs to be valid JSON.

Now, whenever you open a LaTeX file, you should be able to hit F1 to build it. If for some reason it doesn't work (if, for example, you have other build systems for LaTeX installed by plugins like LaTeXTools), then just select Tools -> Build Systems -> XeLaTeX, and everything should work properly.

like image 23
MattDMo Avatar answered Sep 28 '22 05:09

MattDMo