How to place dots at the beginning and end of the selected word as well as quotation marks

Asked

Viewed 183 times

1

I would like to know how I can make Sublime Text 3 add a point . at the beginning and end of the word I selected, in the same way as with the characters ' and ".

When we select a word in Sublime and press on the keyboard the quote button, single or double, instead of deleting the word and replacing it with quotes (as it is done in other editors), it puts a quote at the beginning and at the end of the word, example:

exemplo => 'exemplo'

I wish that when I selected the word and pressed the dot character, ., that he did the same thing:

exemplo => .exemplo.

Instead of erasing the word and leaving only the point as follows exemplo = ..

I tried searching the settings and key_binding of Sublime, but I did not find.

What can I do to add this functionality?

1 answer

2


Just go to shortcut key preferences: Preferences Key Bindings.

In preferences default (editor on the left) you will see that there are already options for "Self-epair" for quotes, parentheses, etc. Then just copy one of them to your preferences (editor on the right side), changing the character indicated for the dot. Stay like this:

{ "keys": ["."], "command": "insert_snippet", "args": {"contents": ".$0."}, "context":
    [
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": false},
        { "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.single - punctuation.definition.string.end", "match_all": true }
    ]
},
{ "keys": ["."], "command": "insert_snippet", "args": {"contents": ".${0:$SELECTION}."}, "context":
    [
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": false }
    ]
},
{ "keys": ["."], "command": "move", "args": {"by": "characters", "forward": true}, "context":
    [
        { "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 },
        { "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
        { "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.single - punctuation.definition.string.end", "match_all": true },
    ]
},
{ "keys": ["backspace"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Left Right.sublime-macro"}, "context":
    [
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "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 },
        { "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
        { "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.single - punctuation.definition.string.end", "match_all": true },
    ]
},

Basically, I copied one of the existing settings (picked from the quotes) and changed the quotes to point: in "keys": ["."], "contents": ".$0.", and other places. One detail is that at some points there are regular expressions, and so I needed to escape the point with \ (since the point has special meaning in regex and it is necessary to make the escape). But since the value is inside a string, I needed to write the \ as \\ (as in "operand": "^\\.", for example).

With this, it is now possible to place the dot around the selected text:

inserir a descrição da imagem aqui


I also changed some values of true for false (in the first 2 configurations of keys):

{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": false}

If you leave "operand" and "match_all" as true, when typing a single point (with no text selected), he puts another point right after (as with the quotation marks or parentheses, which he automatically puts the closure). Switching to false, I avoid this behavior.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.