I am using Python but some pre-defineted snippets. I was wondering is it possible to apply them if the keyword
is the first word.
if
and [TAB]
converted into if cond:
. I want this behaviour if the keyword is the beginning of the line as first word.
Let's assume if I type if
inside a double quotes "
...if[curoser]
..."
and press TAB
, or object.if[TAB]
, then the snippet still applied, which I don't want.
Example cursor is right next to if
and I press TAB
print("if")
^
=> Converted into following piece of code, which I don't want:
print("if cond:
")
or object.if
TAB converted into
object.if cond:
.emacs
file – alper Jun 09 '21 at 15:55wgrep
andmultiple-cursors
to make changes to multiple files in one fell swoop. I don't know whether there is another solution beyond the linked proposed duplicate thread cited above. You may or may not want to change multiple files given your particular needs ... you could start off by modifying just the snippets that should always activate only at the beginning of the line ... – lawlist Jun 09 '21 at 16:22doom-snippets
? if yes you may need some small configuration to work correctly in non-doom emacs. – Ian Jun 10 '21 at 12:20doom-snippets
. Please note that I am usingGNU Emacs 26.3
terminal along withyasnippet-classic-snippets
andyasnippet-snippets
. Those exists under/usr/share/yasnippet-snippets/python-mode
and I have my local snippets under~/.emacs.d/snippets/python-mode
. I get confused where should I manually change each snippet adding# condition: (and (looking-back
to each snippet? – alper Jun 10 '21 at 13:18# --
, the lines above it count as comments, some of which can be directives (or meta data). Snippet directives look like# property: value
and tweak certain snippets properties described below. If no# --
is found, the whole file is considered the snippet template." If you have not already read the tutorial, consider doing so: https://joaotavora.github.io/yasnippet/snippet-development.html#org5e87ae3 Add your# condition: ...
anywhere above the line# --
in the snippet file. If there is no# --
, then add that below the comments. – lawlist Jun 10 '21 at 14:56# condition: (looking-back "^f" nil)
only applies iff
at the beginnig of the line. Can we also do something like allow<white-space>f
too? like if there is tab or space beforef
still snippet could be applied. But prevent if its in between a string like"f"
– alper Jun 11 '21 at 15:19"^[\s\t]?+f"
as the first argument tolooking-back
. The\s
in brackets is a space. The\t
in brackets is a tab. The?
means that what is in brackets may or may not be present in what is being matched. The+
means that there may be more than one space and/or more than one tab. – lawlist Jun 11 '21 at 17:55