Quotation mark completion with f-strings in Sublime Text's Python 3
up vote
0
down vote
favorite
When I type a quotation mark in Sublime Text 3 (3176) it auto completes with a closing quotation mark.
e.g. I type " I get "<cursor>"
This is great and I now expect it all the time. However with the introduction of f-strings into Python if I type f" I get f"<cursor> rather than f"<cursor>". This is not a large problem by any means but it's not as fluid as I feel it could be.
I think the autocompletion rules doesn't add an extra quotation mark if there is a character to the left of the cursor as that's generally when you're trying to input a closing quotation mark.
Is there a way to modify the rules so that it will input the closing quotation mark if the character to the left is an "f"? To avoid a weird use case when you are genuinely trying to end a string finishing with an "f" perhaps there could be an and condition to check for an opening bracket, white space, or equality sign to the left of the "f" for the high-use cases of print(f"string", foo = f"string", and foo =f"string".
python python-3.x autocomplete sublimetext3
add a comment |
up vote
0
down vote
favorite
When I type a quotation mark in Sublime Text 3 (3176) it auto completes with a closing quotation mark.
e.g. I type " I get "<cursor>"
This is great and I now expect it all the time. However with the introduction of f-strings into Python if I type f" I get f"<cursor> rather than f"<cursor>". This is not a large problem by any means but it's not as fluid as I feel it could be.
I think the autocompletion rules doesn't add an extra quotation mark if there is a character to the left of the cursor as that's generally when you're trying to input a closing quotation mark.
Is there a way to modify the rules so that it will input the closing quotation mark if the character to the left is an "f"? To avoid a weird use case when you are genuinely trying to end a string finishing with an "f" perhaps there could be an and condition to check for an opening bracket, white space, or equality sign to the left of the "f" for the high-use cases of print(f"string", foo = f"string", and foo =f"string".
python python-3.x autocomplete sublimetext3
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
When I type a quotation mark in Sublime Text 3 (3176) it auto completes with a closing quotation mark.
e.g. I type " I get "<cursor>"
This is great and I now expect it all the time. However with the introduction of f-strings into Python if I type f" I get f"<cursor> rather than f"<cursor>". This is not a large problem by any means but it's not as fluid as I feel it could be.
I think the autocompletion rules doesn't add an extra quotation mark if there is a character to the left of the cursor as that's generally when you're trying to input a closing quotation mark.
Is there a way to modify the rules so that it will input the closing quotation mark if the character to the left is an "f"? To avoid a weird use case when you are genuinely trying to end a string finishing with an "f" perhaps there could be an and condition to check for an opening bracket, white space, or equality sign to the left of the "f" for the high-use cases of print(f"string", foo = f"string", and foo =f"string".
python python-3.x autocomplete sublimetext3
When I type a quotation mark in Sublime Text 3 (3176) it auto completes with a closing quotation mark.
e.g. I type " I get "<cursor>"
This is great and I now expect it all the time. However with the introduction of f-strings into Python if I type f" I get f"<cursor> rather than f"<cursor>". This is not a large problem by any means but it's not as fluid as I feel it could be.
I think the autocompletion rules doesn't add an extra quotation mark if there is a character to the left of the cursor as that's generally when you're trying to input a closing quotation mark.
Is there a way to modify the rules so that it will input the closing quotation mark if the character to the left is an "f"? To avoid a weird use case when you are genuinely trying to end a string finishing with an "f" perhaps there could be an and condition to check for an opening bracket, white space, or equality sign to the left of the "f" for the high-use cases of print(f"string", foo = f"string", and foo =f"string".
python python-3.x autocomplete sublimetext3
python python-3.x autocomplete sublimetext3
edited Nov 12 at 8:43
khelwood
29.8k74062
29.8k74062
asked Nov 12 at 8:19
m4p85r
304115
304115
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Yes, this is possible. Simply add the following to your User keymap file (Preferences menu -> Key Bindings. The User keymap is the one on the right hand side):
// Auto-pair quotes even after string modifiers.
// Copied over from the default bindings with modifications to `preceding_text`
// and an added selector condition.
{ "keys": ["""], "command": "insert_snippet", "args": {"contents": ""$0""}, "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": "^(?:t| |\)|]|\}|>|$)", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "(?i)\b[bfru]+$", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "source.python" },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.double - punctuation.definition.string.end", "match_all": true }
]
},
{ "keys": ["'"], "command": "insert_snippet", "args": {"contents": "'$0'"}, "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": "^(?:t| |\)|]|\}|>|$)", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "(?i)\b[bfru]+$", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "source.python" },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.single - punctuation.definition.string.end", "match_all": true }
]
},
This will be shipped by default in a future build of ST.
It uses a scope selector to determine whether the caret is already inside a string or not, so the case of finishing a string with an f character isn't a problem.
Brilliant! Thank you. Worked exactly as expected.
– m4p85r
Nov 12 at 8:34
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258196%2fquotation-mark-completion-with-f-strings-in-sublime-texts-python-3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Yes, this is possible. Simply add the following to your User keymap file (Preferences menu -> Key Bindings. The User keymap is the one on the right hand side):
// Auto-pair quotes even after string modifiers.
// Copied over from the default bindings with modifications to `preceding_text`
// and an added selector condition.
{ "keys": ["""], "command": "insert_snippet", "args": {"contents": ""$0""}, "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": "^(?:t| |\)|]|\}|>|$)", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "(?i)\b[bfru]+$", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "source.python" },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.double - punctuation.definition.string.end", "match_all": true }
]
},
{ "keys": ["'"], "command": "insert_snippet", "args": {"contents": "'$0'"}, "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": "^(?:t| |\)|]|\}|>|$)", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "(?i)\b[bfru]+$", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "source.python" },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.single - punctuation.definition.string.end", "match_all": true }
]
},
This will be shipped by default in a future build of ST.
It uses a scope selector to determine whether the caret is already inside a string or not, so the case of finishing a string with an f character isn't a problem.
Brilliant! Thank you. Worked exactly as expected.
– m4p85r
Nov 12 at 8:34
add a comment |
up vote
2
down vote
accepted
Yes, this is possible. Simply add the following to your User keymap file (Preferences menu -> Key Bindings. The User keymap is the one on the right hand side):
// Auto-pair quotes even after string modifiers.
// Copied over from the default bindings with modifications to `preceding_text`
// and an added selector condition.
{ "keys": ["""], "command": "insert_snippet", "args": {"contents": ""$0""}, "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": "^(?:t| |\)|]|\}|>|$)", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "(?i)\b[bfru]+$", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "source.python" },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.double - punctuation.definition.string.end", "match_all": true }
]
},
{ "keys": ["'"], "command": "insert_snippet", "args": {"contents": "'$0'"}, "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": "^(?:t| |\)|]|\}|>|$)", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "(?i)\b[bfru]+$", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "source.python" },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.single - punctuation.definition.string.end", "match_all": true }
]
},
This will be shipped by default in a future build of ST.
It uses a scope selector to determine whether the caret is already inside a string or not, so the case of finishing a string with an f character isn't a problem.
Brilliant! Thank you. Worked exactly as expected.
– m4p85r
Nov 12 at 8:34
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Yes, this is possible. Simply add the following to your User keymap file (Preferences menu -> Key Bindings. The User keymap is the one on the right hand side):
// Auto-pair quotes even after string modifiers.
// Copied over from the default bindings with modifications to `preceding_text`
// and an added selector condition.
{ "keys": ["""], "command": "insert_snippet", "args": {"contents": ""$0""}, "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": "^(?:t| |\)|]|\}|>|$)", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "(?i)\b[bfru]+$", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "source.python" },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.double - punctuation.definition.string.end", "match_all": true }
]
},
{ "keys": ["'"], "command": "insert_snippet", "args": {"contents": "'$0'"}, "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": "^(?:t| |\)|]|\}|>|$)", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "(?i)\b[bfru]+$", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "source.python" },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.single - punctuation.definition.string.end", "match_all": true }
]
},
This will be shipped by default in a future build of ST.
It uses a scope selector to determine whether the caret is already inside a string or not, so the case of finishing a string with an f character isn't a problem.
Yes, this is possible. Simply add the following to your User keymap file (Preferences menu -> Key Bindings. The User keymap is the one on the right hand side):
// Auto-pair quotes even after string modifiers.
// Copied over from the default bindings with modifications to `preceding_text`
// and an added selector condition.
{ "keys": ["""], "command": "insert_snippet", "args": {"contents": ""$0""}, "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": "^(?:t| |\)|]|\}|>|$)", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "(?i)\b[bfru]+$", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "source.python" },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.double - punctuation.definition.string.end", "match_all": true }
]
},
{ "keys": ["'"], "command": "insert_snippet", "args": {"contents": "'$0'"}, "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": "^(?:t| |\)|]|\}|>|$)", "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "(?i)\b[bfru]+$", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "source.python" },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.single - punctuation.definition.string.end", "match_all": true }
]
},
This will be shipped by default in a future build of ST.
It uses a scope selector to determine whether the caret is already inside a string or not, so the case of finishing a string with an f character isn't a problem.
edited Nov 12 at 8:47
khelwood
29.8k74062
29.8k74062
answered Nov 12 at 8:28
Keith Hall
10.4k22853
10.4k22853
Brilliant! Thank you. Worked exactly as expected.
– m4p85r
Nov 12 at 8:34
add a comment |
Brilliant! Thank you. Worked exactly as expected.
– m4p85r
Nov 12 at 8:34
Brilliant! Thank you. Worked exactly as expected.
– m4p85r
Nov 12 at 8:34
Brilliant! Thank you. Worked exactly as expected.
– m4p85r
Nov 12 at 8:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258196%2fquotation-mark-completion-with-f-strings-in-sublime-texts-python-3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown