Tomas Aschan
Tomas Aschan

Reputation: 60584

"Smart quotes" in Sublime Text 2 with LaTeXTools

I've recently migrated my LaTeX-authoring from TeXWorks to Sublime Text 2, and one feature which I'm really missing is the ability to type "regular quotes", (using shift+2 on my Swedish keyboard, yielding "quoted text") and having the editor automatically convert them to the correct way of quoting in LaTeX, which is ``quoted text'' while I'm typing.

I've tried to look for a way to do this in ST2, but most of what I find is related to escaping quotes in strings automatically, which is not what I'm after.

Is there a way to get this functionality in ST2?

Upvotes: 2

Views: 3535

Answers (3)

RossBille
RossBille

Reputation: 1489

Install the Latexing package it doesn't list this as a feature but it automatically converts " to ``|'' with the caret in the center ready for your quoted text. Pressing " will then move the caret past the closing ``''|.

Upvotes: 1

angerson
angerson

Reputation: 7402

You might be able to make Sublime Text 2 give quotes a once-over you're finished writing them, but since that would probably involve writing a complicated plugin, why not remap the " key to insert the correct characters instead?

It's possible to add custom key bindings which mimic the default automatic " pairing but instead insert LaTeX quotes where appropriate. Add these lines (sourced from Preferences -> Key Bindings – Default, lines 272–293) to your Preferences -> Key Bindings – User file:

{ "keys": ["\""], "command": "insert_snippet", "args": {"contents": "``$0''"}, "context":
    [
        { "key": "selector", "operator": "equal", "operand": "text.tex.latex"},
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|>|$)", "match_all": true },
        { "key": "preceding_text", "operator": "not_regex_contains", "operand": "[\"a-zA-Z0-9_]$", "match_all": true },
        { "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.double", "match_all": true }
    ]
},

{ "keys": ["\""], "command": "insert_snippet", "args": {"contents": "``${0:$SELECTION}''"}, "context":
    [
        { "key": "selector", "operator": "equal", "operand": "text.tex.latex"},
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
    ]
},

{ "keys": ["\""], "command": "move", "args": {"by": "words", "forward": true}, "context":
    [
        { "key": "selector", "operator": "equal", "operand": "text.tex.latex"},
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "following_text", "operator": "regex_contains", "operand": "^''", "match_all": true }
    ]
},

The first chunk of code overwrites Sublime Text's default quote-pairing and adapts it for LaTeX-styled quotes. When you type a " within a LaTeX file, as long as it's in a location that would normally insert a set of double quotes, you'll get this:

``|''

The second section replaces the default functionality for automatically enclosing selected text with quotes. In a LaTeX file, selecting text and then pressing " will result in this:

``This is the text you selected|''

The final re-binding skips over ending quotes ('') when you press " while the caret is adjacent to them. That is, when you press " here:

``Sublime Text is the best!|''

The caret will move outside of the quotes, like this:

``Sublime Text is the best!''|

Upvotes: 10

Bibhas Debnath
Bibhas Debnath

Reputation: 14939

The shortest way I see is using a snippet. This one will work -

<snippet>
    <content><![CDATA[
``${1:this}''
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>lq</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <scope>text.tex.latex</scope>
</snippet>

Save this as latex-quote.sublime-snippet in Packages > User under ST2's directory. Then at any point you want to use the quotes, just write lq(I named it from LaTeX quote) and press Tab.

Upvotes: 0

Related Questions