Sublime Forum

Auto-Pair Quotes

#1

I have been hand coding everything for years and am not at all used to my editor automatically completing quote and bracket pairs as I’m typing . . . I am curious, before I just disable that functionality, how other people use it. I must be overlooking something. I don’t find it saves me any keystrokes ( I still have to use arrow keys or the way-on-the-other-side-of-the-keyboard End key to get out of the quote pair, so if anything it’s just pre-emptively preventing me from leaving it open. Perhaps there’s a “Hey I’m done, get me out of here” keystroke, to go from inside the pair to outside, but barring that . . . what is the utility to auto pairing quotes and brackets? It just drives me batty, personally.

0 Likes

Two quick questions about Sublime Text
#2

That’s one reason. Also not always your typing quotes and then you continue typing. Also this works with selections so if you want to quote something really quick just select and type a quote it’ll wrap the selection in quotes. If you are used to the habit of just inserting the quotes manually u can have that without disabling the functionality because if you do insert a quote while there’s another quote in front it will just jump forward (right arrow functionality). I mean I really don’t see any disadvantage from a productivity point of view but I might be wrong. On the contrary I see it speeds up your productivity a bit. :smile:

However here’s how to disable it…

Menu > Preferences > User file preferences:
Insert the following

# Set to false to stop auto pairing quotes, brackets etc autoMatchEnabled false

0 Likes

#3

So it doesn’t seem I’m really missing much, in terms of working with it – it just doesn’t fit into my work flow too well. In my case, because I am so accustomed to closing them myself, I find I actually end up having to manually clean up afterwards. It’s not such a big deal with the quotes but with the brackets, when they are added with new lines etc.

0 Likes

#4

I find the main disadvantage to disabling the auto-pair quote function is stressing out your syntax highlighting. This is especially obvious if you are editing large files with plenty of text after the cursor. If you type one quote, and it’s not auto-paired, there is a noticeable delay while the rest of the document is checked and then highlighted as a string. Once you add the second quote, the rest of the document returns to normal.

With auto-pairing, the two quotes are inserted instantly and the highlighting stays sane. That being said, I’ve turned off auto-pairing of quotes as well due to conflicts with my muscle-memory :wink:

0 Likes

#5

This should no longer be as issue after the 20091113 beta, as syntax highlighting will no longer be evaluated past what’s visible. There are still speed benefits to auto pairing quotes, but hopefully not perceptible ones.

0 Likes

#6

well that’s the beauty of sublime, it fits everybody and you can customize it to your specific needs and make it dance and sing to you all day if that’s what you want :smile: it’s a sublime experience :stuck_out_tongue:

0 Likes

#7

I have defined the following keybinding for this very same thing:

<binding key="p,p" command="move characters 1"> <context name="allSelectionsEmpty" value="true" /> <context name="allFollowingText" value="^\)\]\}\>\.,;:'&quot;]" /> </binding>

Now every time I press the sequence “pp” before one of those characters, the cursor moves past them without me having to reach for the arrow keys.

Question: I haven’t figured out yet how to escape a double quote… " doesn’t seem to work…

EDIT: Added " as Jon suggests.

0 Likes

#8

You can use "

0 Likes

#9

Hi, I’m feeling the same as Allen and guillermooo here about auto-pairing. So I tried to add the key binding that guillermooo suggested back in 2010, but it looks like the syntax has changed since then (or have I missed something ?).

Can you help me with this key binding ? It’s not parsed correctly by ST2.

{ "keys": "p,p"], "command": "move characters 1", "context":
	
		{ "name": "allSelectionsEmpty", "value": true },
		{ "name": "allFollowingText", "value": "^\)\]\}\>\.,;:'&quot;]" }
	]
}
0 Likes

#10

What would make this work flow even better is the ability to “tab out” of the auto-matched quote/brace/bracket/ending mark. There is a plug-in for Visual Studio that does this and it’s incredibly useful. When you get the point where you’d normally type another quote or right arrow you instead tab.

The productively comes from the fact that tabbing is much faster than either right-arrow or another closing character, many of which require a shift+combo. I think I’ve started one or two thread in these forums requeting this feature as have a few others. You don’t know what you’re missing until it’s gone :smile:

0 Likes

#11

@shawnoster:

Add the following to your user keybindings: [code] { “keys”: “tab”], “command”: “move”, “args”: {“by”: “characters”, “forward”: true}, “context”:

    { "key": "following_text", "operator": "regex_contains", "operand": "^)\"'\\]\\};]", "match_all": true },
    { "key": "auto_complete_visible", "operator": "equal", "operand": false },
    { "key": "has_next_field", "operator": "equal", "operand": false }
]   

}[/code]

0 Likes

#12

This binding eats tab when I try to indent blocks such as:

* { color:fff; }

Instead of indenting, hitting tab moves the caret after the }. Is there a workaround for this?

Edit:

If it doesn’t complicate things too much I think it might also make sense for tabbing out of quotes to work with a select, so that:

  • you can select: TEXT
  • hit " to wrap it in quotes: "TEXT"
  • and then hit TAB to place the caret after the last " (rather than replacing the selection with a tab)

But the indent issue is more pressing.

0 Likes

#13

This used to drive me nuts too until I figured out one particular item: ignore the “paired” quote, bracket, brace, whatever that ST2 inserts for you. Just keep typing like you normally would and you won’t end up with double closing things. So, if you want to type:
“test me now”
you would type quote, test me now, quote
and you’ll get the above string and find yourself on the outside of the quotes.

It’s basically just an easy way to make sure you don’t miss a closing quote or bracket.

0 Likes

#14

@MystickPhoenix I think that’s the way you’re supposed to used it, but if it’s possible to get the tab functionality working well, I (think) I’d prefer it. Mashing the tab key is easier/faster than shift+anything.

I’m currently experimenting with auto-pairing asterisks when writing Markdown and I find shift+8 to be a particularly disrupting binding. But I’m nitpicking, of course.

0 Likes

#15

Is there any way to disable auto pairing, but still be able to quote the selected text with ’ and "? They seem mostly unrelated, but disabling one disabled the other.

I also never understood auto pairing. Basically no time savings, but definitely can mess you up at times by inserting extra quotes where you don’t want them.

0 Likes

#16

@textisgood

Auto-pairing quotes works via some rather ingenious key bindings (try searching for “auto-pair” in your default bindings file).

Just add these to your user key binding file for quoting selected text with " and':

{ "keys": "\""], "command": "insert_snippet", "args": {"contents": "\"${0:$SELECTION}\""}, "context":
	
		{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
	]
},
{ "keys": "'"], "command": "insert_snippet", "args": {"contents": "'${0:$SELECTION}'"}, "context":
	
		{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
	]
},

Note that I’ve removed out the context that requires the “auto_match_enabled” setting to be true, which means that these will work regardless of your settings. Also, I pulled them from the default bindings file for Linux; I don’t imagine there would be differences between OSs in this case, but I could be wrong.

0 Likes

#17

[quote=“shawnoster”]What would make this work flow even better is the ability to “tab out” of the auto-matched quote/brace/bracket/ending mark. There is a plug-in for Visual Studio that does this and it’s incredibly useful. When you get the point where you’d normally type another quote or right arrow you instead tab.

The productively comes from the fact that tabbing is much faster than either right-arrow or another closing character, many of which require a shift+combo. I think I’ve started one or two thread in these forums requeting this feature as have a few others. You don’t know what you’re missing until it’s gone :smile:[/quote]

{ "keys": "tab"], "command": "move", "args": {"by": "characters", "forward": true}, "context": { "key": "setting.command_mode", "operand": false }, { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }, { "key": "preceding_text", "operator": "regex_contains", "operand": "^^]", "match_all": true}, { "key": "following_text", "operator": "regex_contains", "operand": "(?:\"|\\)|\\]|\\}|\\>|')", "match_all": true} ] }

0 Likes

#18

I was about to post my own attempt to improve C0D312’s binding, but @prmaple’s looks more robust. Thanks for posting!

0 Likes

#19

Mine is a bit more complicated, but features a lot of behaviors that I like.

[code]{ “keys”: “tab”], “command”: “move”, “args”: {“by”: “characters”, “forward”: true}, “context”:

    { "key": "selection_empty", "operator": "equal", "operand": true },
    { "key": "preceding_text", "operator": "not_regex_match", "operand": ":space:]]*", "match_all": true },
    { "key": "following_text", "operator": "regex_contains", "operand": "^\"'\\)\\}\\]]", "match_all": true },
    { "key": "auto_complete_visible", "operator": "equal", "operand": false },
    { "key": "has_next_field", "operator": "equal", "operand": false }
]

},
{ “keys”: “tab”], “command”: “move”, “args”: {“by”: “words”, “forward”: true}, “context”:

    { "key": "selection_empty", "operator": "equal", "operand": true},
    { "key": "preceding_text", "operator": "not_regex_match", "operand": ":space:]]*", "match_all": true },
    { "key": "following_text", "operator": "regex_contains", "operand": "^.,:;]", "match_all": true },
    { "key": "auto_complete_visible", "operator": "equal", "operand": false },
    { "key": "has_next_field", "operator": "equal", "operand": false }
]

},
{ “keys”: “tab”], “command”: “move”, “args”: {“by”: “characters”, “forward”: true}, “context”:

    { "key": "selection_empty", "operator": "equal", "operand": true},
    { "key": "selector", "operator": "equal", "operand": "text.tex.latex", "match_all": true },
    { "key": "preceding_text", "operator": "not_regex_match", "operand": ":space:]]*", "match_all": true },
    { "key": "following_text", "operator": "regex_contains", "operand": "^$]", "match_all": true },
    { "key": "auto_complete_visible", "operator": "equal", "operand": false },
    { "key": "has_next_field", "operator": "equal", "operand": false }
]

}[/code]

The other keybindings I’ve found tend to not factor in whether the bracket or quote is at the beginning of the line, so they didn’t work for me. I want ^|"Hello!" to become ^ |"Hello" instead of ^"|Hello" (’^’ is the start of the line, and ‘|’ is the cursor position). When I’m at the beginning of the line, I almost always want to indent, so I’ve factored that in. I also use tab to skip over common punctuation, and in that case, I want to skip to the next word, not just one character to the right. And finally, I write quite a bit in LaTeX, so I added a special case for LaTeX’s inline math.

Hopefully, this’ll be of use to someone.

0 Likes

#20

How might I tab out of a selection that I have wrapped with auto-pairs. Like this:

here are some words

I place the cursor on “some” and hit Ctrl-D to select, then hit " to wrap like so:

here are "some|"] words

The cursor is where the | is but the word some is still selected. Hitting tab would move past the closing quote:

here are "some"|] words

Hitting tab again would move past the closing square bracket:

here are "some"]| words

And if the cursor is here:

here are "|some"] words

The hitting tab would still move it past the closing quote as above:

here are "some"|] words

Possible?

0 Likes