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:

alper
  • 1,370
  • 1
  • 13
  • 35
  • @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 09 '21 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 09 '21 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 '21 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 '21 at 13:18
  • "Optionally, if the file contains a line of # --, 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
  • @lawlist # condition: (looking-back "^f" nil) only applies if f at the beginnig of the line. Can we also do something like allow <white-space>f too? like if there is tab or space before f still snippet could be applied. But prevent if its in between a string like "f" – alper Jun 11 '21 at 15:19
  • 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 '21 at 17:55

0 Answers0