Sublime Forum

Create auto brackets for specific symbol but only in certain files

#1

Hello, I want that $ auto closes (like (),[],etc), but only in .tex, .md and .Rmd files.

So far, I have this that does the job, but across all file types:

<snippet><content><![CDATA[
\$${0:$SELECTION}\$
]]></content></snippet>

in autopeso.sublime-snippet, and this as key shortcut:

{ 
        "keys": ["$"], 
        "command": "insert_snippet", 
        "args": {"name": "Packages/User/autopeso.sublime-snippet" }
    } 

How do I modify what I have to achieve what I want? Or do I need an entirely different setup?

0 Likes

#2

You don’t need to create a separate snippet file. The insert_snippet command is enough for the task.
Here is the relevant keybinding for it

{
    "keys": ["$"],
    "command": "insert_snippet",
    "args": { "contents": "\\$$0\\$" },
    "context": [
        {
            "key": "selector", "operator": "equal", "operand": "text.tex, text.html.markdown"
        }
    ]
},

The context here restricts the keybinding for only LaTeX files (text.tex) and Markdown files (text.html.markdown). I am not sure what an .Rmd file is but you can check its base scope using Tools -> Developer -> Show Scope Name when you have an .Rmd file open and check it’s base scope and put it in the context accordingly.

2 Likes

#3

Cool!!! thank you so much!

0 Likes