0

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:


7
  • @lawlist I get confust how to apply those solution to all the native/local snippets. Should I manuall change them one by one? or which changes should I make to my .emacs file
    – alper
    Jun 9 at 15:55
  • When the need arises, I personally use a combination of wgrep and multiple-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 9 at 16:22
  • Do you use doom-snippets? if yes you may need some small configuration to work correctly in non-doom emacs.
    – Ian
    Jun 10 at 12:20
  • No I am not using doom-snippets. Please note that I am using GNU Emacs 26.3 terminal along with yasnippet-classic-snippets and yasnippet-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 at 13:18
  • 1
    Perhaps you could try a regexp like "^[\s\t]?+f" as the first argument to looking-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 at 17:55

Browse other questions tagged or ask your own question.