When using f strings in Python 3 for Atom, it doesn't autocomplete the string correctly. Typing in
types_of_people = 10
x = f"There are {types_of_people} types_of_people."
I get x = f"...
when I start typing but the end quote doesn't autocomplete
When I type in the ending quote I get x = f"There are {types_of_people} types_of_people."""
How can I get the end quote to autocomplete as desired?
I went to this link. But atom still prints additional quotes when I am typing the end quote, instead of just giving the end quote.
Approach 1
Add a snippet to autocomplete the f-string
as suggested here.
You can add a snippet by editing the snippets.cson
file found in %USERPROFILE%/.atom
directory. It can also be edited by selecting Snippets...
under Edit
menu.
While editing the file, type snip
and press TAB. It should generate a sample config like this :
'.source.js':
'Snippet Name':
'prefix': 'Snippet Trigger'
'body': 'Hello World!'
Edit the above to this :
'.source.python':
'f-string':
'prefix': 'f"'
'body': 'f"$1"'
The auto-complete of the f-string
in this approach is triggered only on pressing TAB after typing f"
Approach 2
Add the following lines to the respective config file for your atom editor :
init.coffee
atom.commands.add 'atom-text-editor', 'custom:insert-double-quotes', ->
# Get the active text editor instance
editor = atom.workspace.getActiveTextEditor()
# Get the character under the current position of the cursor
currentCharacterPrefix = editor.getLastCursor().getCurrentWordPrefix().slice(-1)
# Check if the character prefix is 'f'
if(currentCharacterPrefix == 'f')
# Insert double quotes with cursor position set in between the double quotes
snippetBody = '\"$1\"'
atom.packages.activePackages.snippets?.mainModule?.insert snippetBody
else
# If prefix is not 'f', then insert a double quote
# as if entering it manually in the text editor
# (so bracket-matcher does it's autocomplete job)
editor.insertText("\"")
keymap.cson
# Set a key binding for double quote to trigger the command in the init script
'atom-text-editor[data-grammar="source python"]':
'\"': 'custom:insert-double-quotes'
The config file init.coffee
file can be be edited by selecting the Init Script...
option under Edit
menu and keymap.cson
file can be edited by selecting the Keymap...
option under Edit
menu. These config files are found under %USERPROFILE%/.atom
directory.
Close and reopen the atom editor after editing the config files. In the editor (for python specific files), type f"
and it should auto-complete to f""
. The cursor position should be in between the two double quotes.
This approach is inspired by this answer here.
In Approach 2, there is another way to make bracket-matcher package think that it is just adding normal bracket pairs. (No need to disable the autocomplete for ""
in bracket matcher)
Add following lines to the init.coffee
config file:
atom.commands.add 'atom-text-editor', 'custom:insert-double-quotes', ->
# Get the active text editor instance
editor = atom.workspace.getActiveTextEditor()
# Get the character under the current position of the cursor
currentCharacterPrefix = editor.getLastCursor().getCurrentWordPrefix().slice(-1)
# Check if the character prefix is 'f'
if(currentCharacterPrefix == 'f')
# Fool the Bracket Matcher package by inserting a space
editor.insertText(" ")
# Insert a double quote for bracket matcher to handle auto-complete job
editor.insertText("\"")
# Set cursor position to remove the space
editor.getLastCursor().moveLeft(1)
editor.backspace()
# Set cursor position in between the double quotes
editor.getLastCursor().moveRight(1)
else
# If prefix is not 'f', then insert a double quote
# as if entering it manually in the text editor
# (so bracket-matcher does it's autocomplete job)
editor.insertText("\"")