Simple Templates
-
For the past few years I have been using an old text editor PFE (Programmer’s File Editor) and have found it fairly satisfactory despite its age. However, recently I have turned to experiment with Notepad++. I miss PFE’s multiple windows (rather than tabs but that is merely a matter of familiarity) but what I mainly miss is the ability to create and insert Templates. One can do it by cutting and pasting from a template file but is there a more straightforward method? What do the rest of you do?
-
@lawriehodges
there are plugins like snippet, snippet plus, finger text … available which
can be used to achieve this as long as you are using the 32bit version of npp.I (is it I or is it me - I always get confused about this) personally use python script
which loads a file with the snippets as a dictionary.Cheers
Claudia -
It’s “I”…I’ve been taught to follow this simple rule:
The correct one is the one that sounds right:
“Me personally use pythonscript which loads…” – NOPE!
“I personally use pythonscript which loads…” – YES!
I think you know more about it than you realize, as you didn’t say “Me always get confused about this”!Can you give an example of how you use pythonscript with the snippets file?
-
Hi Scott,
The correct one is the one that sounds right:
:-) for a native speaker this might be true but for a foreigner … :-)
I can’t really recall what my English teacher told me about that but
I have something in my mind that there were some rules about when using
me and I - something like if an adverb is followed than it is “I” but if the pronoun
is after the adverb than it is me - as said - too long ago - can’t really remember.In regards to the snippet script. It isn’t something fancy.
The script itself_g = globals() if not _g.get('SNIPPET_LOADED'): snippet_dict = {} snippet_file = r'FULL_FILE_PATH' with open(snippet_file, 'rb') as f: key = None for line in f: pos = line.find('::') if ( pos > -1) : if key is None: key = line[:pos] value = line[pos+2:] else: snippet_dict[key] = value key = line[:pos] value = line[pos+2:] else: value += line snippet_dict[key] = value SNIPPET_LOADED = True curpos = editor.getCurrentPos() editor.setTargetEnd(curpos) snippet = editor.getWord() len_word = len(snippet) editor.setTargetStart(curpos-len_word) replacement = snippet_dict.get(snippet) if replacement: length = editor.replaceTarget(replacement) curpos = editor.getCurrentPos() editor.gotoPos(curpos+length)
and a text file like
utf8::#!/usr/bin/env python # -*- coding: utf-8 -*- main::if __name__ == '__main__': pass miniapp::if __name__ == '__main__': class DemoFrame(wx.Frame): def __init__(self, *args, **kwargs): super(DemoFrame, self).__init__(*args, **kwargs) self.Show() app = wx.App(redirect=False) DemoFrame(None, wx.ID_ANY, title='Test', size=(300, 200)) app.MainLoop() app = None
Assign a shortcut to the script and type
utf8
in the editor, press shortcut and utf8 gets replaced by the text
#!/usr/bin/env python
# -- coding: utf-8 --I’ve put this into startup script and to prevent that it reads the file everytime
you want to use a snippert I check if SNIPPET_LOADED is true.
If you update your snippet file, set the variable through the console to False.Cheers
Claudia -
and because I forget what keys I’ve used I created a little shortcut
which is also in the startup.pysd = lambda: console.write('\n'.join(snippet_dict.keys()))
So I just have to remember that I need to run sd() in the console :-)
Cheers
Claudia -
Oh, okay, I get it now…a quick custom snippets thingy… From your reply to the OP, I thought maybe it was something different, that integrated with the real snippets plugins that you mentioned somehow. But…I like it! Sometimes a real plugin is “too heavy” and a simpler pythonscript is just right! :)
It is interesting, your use of _g = globals() at the top. I do a similar thing this way:
try: something_loaded except NameError: something_loaded = False if not something_loaded: #....do one-time init here... something_loaded = True
I guess whatever gets the job done! :)
Sometimes pythonscript requires a bit of non-conventional python thinking, because when pythonscripts end their objects and functions live on, unlike a real python program that is simply gone from memory when it ends.
-
@Claudia-Frank
Thanks for your reply. I’ve loaded the 32 bit version Npp++ and the snippets++ plugin. I have still to experiment. -
@Claudia-Frank
Thanks, once again. It seems that FingerText will do what I want.Incidentally:
I (is it I or is it me - I always get confused about this) personally use python script . . .
It should be ‘I’, since it is the subject of ‘use’. The presence of ‘personally’ (which many would regard as superfluous) does not affect this.Regards,
Lawrie -
Hello Scott,
sorry for late response - really busy at the moment.
Yeah, it is just another snippet solution :-)
and even when I love python, and I use it as main programming/scripting language at the moment,
I still don’t be that pythonic as I should be ;-)
Your solution looks more like python way than mine.Cheers
Claudia -
I created this tool so that I could have an editor independent popup with the snippets I use in any software.