Snippets In KDevelop / Kate
Hey all!
Just wanted to give you a little rundown on Snippets in Kate 4.4 (via the snippets_tng plugin) and KDevelop Beta 8 (soon to be released).
Note: The Kate plugin was written by Jowenn and introduced me to all these nice features. For KDevelop I wrote a somewhat simpler yet imo better implementation. We will try to get the best of both worlds into KDE 4.5. Stay tuned!
General Usage & Features
- create a snippet repository (or download via GHNS [see below])
- create snippets in that repository
- insert snippets via the snippets view (i.e. double click), or (imo better/faster) insert them via code-completion (remember: CTRL + Space requests code completion at the current cursor position!).
- snippet gets inserted (properly indented) and potential placeholders/variables get expanded. A variable is something like
%{date}
or${email}
. Also take a look at the API documentation. - variables that get inserted via “${…}” will be “selectable”, meaning you can jump from one var to the other by hitting TAB / Shift TAB
- the
%{...}
vars will only get expanded and inserted, without getting selectable. - multiple occurrences of the same variable will be updated once one of them gets edited, something that is called “mirroring” in other editors.
- once one edits ESC the cursor is placed at the end of the snippet or to the first occurrence of
${cursor}
or%{cursor}
and the user types something, the snippet-handler quits and you are left with your normal editor until you insert the next snippet - nested snippets (i.e. insert snippet than insert another one) should “just work”.
Snippet Management
group snippets by file type, i.e. PHP snippets will only be offered during code completion when one edits a PHP file.
Note: In KDevelop and KDE 4.4 nested documents are supported, e.g. create a CSS snippet and it will be shown inside the CSS parts of a HTML document or similar. This uses my HighlightInterface I wrote for KDE 4.4. I still have to rewrite some parts of the snippets_tng plugin for Kate so that it works there as well- group snippets in repositories, set an Author and a License of your choice
- publish snippet repositories via GHNS: In Kate you can already download snippets from GHNS but we sadly don’t have any repos up on opendesktop… I’ll have to add some prior to KDE 4.4. Also we didn’t have enough time to implement uploading of Repos from inside Kate in time for KDE 4.4. So stay tuned for KDE 4.5. KDevelop currently has no support for GHNS, but I plan to fix this tomorrow or the next days - together with uploading from inside KDevelop, i.e. all the nice features of GHNS v3.
- in KDevelop (and someday in Kate as well) you can simply select a part of your currently opened document and select the “create snippet from selection” in the ContextMenu - easy & fast!
TODO
There’s much to do.
- Highest priority right now for me is to get GHNS with all bells and whistles supported for KDevelop.
- Then I’ll merge and integrate the Kate & KDevelop plugins as much as possible, so we have a reduce code base.
- Also important is to make all shortcuts configurable
- Another thing is: How could we improve interoperability even between e.g. editors like TextMate or Gedit? Both have snippets features and their bundles are available in the net. If we can support those we’d save us a lot of work
Also, I should probably do a screencast… Not now though ;-)
Comments
Want to comment? Send me an email!
Comment by Igor (not verified) (2013-04-05 07:20:00)
Hi Millian,
first, thank you for your work on Kate.
I use the snippets plugin a lot in KDevelop, but only static snippets, i.e. no variables or scripting. I wonder, do you have any documentation available? Specifically I would like to learn about the variables available and how to write scripts. I want to write a snippet for PHPDOC comments creation. It’s easy to just create a snippet with all the comment fields preset, but I would also like the snippet to insert an asterisk character at the beginning of the line each time I press Enter inside a comment. Can this be done with snippet scripting? Is there any documentation? Are you aware of any existing plugins that would do this thing?
Thank you again!
Igor
Comment by Geo Lours (not verified) (2012-01-18 12:20:00)
Hello,
I use the software “kate” and I want to insert html tags in a document. I can’t find tools to do that. For instance, I want to replace the expression
this is bold
by<strong >this is bold</strong >
. Does that exist?I’ve found the tool “snippets” in the external modules. But I can’t use it. I’ve create a snippet called “strong” with this content:
and this javascript function
But it doesn’t work.
To do that, I’ve had a look in kate’s help file (in particular “scripting API”) and I imitated a snippet which is supplied and which is designed like that:
fonction javascript
and it works
What did I do wrong? What should I modify ?
Comment by Anonymous (2015-08-03 05:54:00)
I found out after explore this problem that object view don’t work in snippets document works good. The object view works goon in comands and identation scripts. So if you want to manipulate with selected text use scripts not snippets! As to html-tag operations there are a lot of scripts I found in my Kate now. I don’t how this problem occurred that time is actual now. But if it is I may send this scripts to you by email. Leave it if necessary.
Comment by Milian Wolff (2012-01-18 21:20:00)
if I understand you, you want to select something and then trigger a snippet to wrap that in a tag? Yes, that is doable, and much easier than you think. Just create a snippet with the following contents:
<strong>%{selection}</strong>
cheers
Comment by Anonymous (2016-03-18 14:37:00)
This does not work for me (Kate Version 3.11.5 Using KDE Development Platform 4.11.5) %{selection} is always empty
Comment by Anonymous (2016-03-31 13:41:00)
Use
${text}
instead of%{selection}
. The following works for me in Kate 3.13.3:Not sure why this isn’t documented anywhere as it’s very useful…
Comment by Anonymous (2016-03-31 14:14:00)
Actually, this doesn’t seem to work anymore either. Would be great to find a solution to this (and also to the bug that prevents shortcuts for snippets from working).
Comment by Geo Lours (not verified) (2012-01-19 16:34:00)
Thanks a lot. It’s OK cheers
Comment by gorn (not verified) (2011-04-23 01:27:00)
Maybe it is a stupid question, but how do I activate snipped from keyboard. I do not like to have to doubleclick the snippet everytime. Ctrl+space is mentioned in the text, but it does nothing.
Comment by Milian Wolff (2012-01-18 21:20:00)
you can also assign shortcuts to single snippets.
Comment by Slava (not verified) (2010-12-14 19:26:00)
I do not agree that ctrl+space is slower then a mouse, just leave
namespace
empty in repository, and name your snippet properly! then I just typefor
(fo
also works), press ctrl+space, and, voila, required snippets are at the top of the list. The only thing that bothers me (apart from textmate users :)) is the snippet text selection color, for us using dark colorschemes result is hardly usable, shell it somehow respect the parent window palette? The same happens to semantic code highlighting and some other smart stuff, but that is still usable as I can tune the level of highlighting.Comment by Jacq-ny-mouse (not verified) (2010-09-13 06:50:00)
Comment by jaymz (not verified) (2010-07-06 01:02:00)
This plugin has made my day! Thank you all for your hard work. I’m really loving this. I use kate all day at work and all evening at home, its my editor for everything and the last thing I was waiting for was snippets like this.
Comment by craig (not verified) (2010-05-09 23:25:00)
Yes, so happy to see this. Thanks for the hard work.
Comment by kamil (not verified) (2010-03-14 13:21:00)
Thanx for your work. It’s very important. Up to now missing snippets in kate was blocking me to dig Mac with TextMate and switch totally to Linux.
Comment by NoSpox (not verified) (2010-02-04 10:34:00)
would like to see compatibility with TextMate’s snippets, and a configurable trigger (CTRL + Space, Tab, …)
Comment by Milian Wolff (2010-02-04 11:25:00)
Yes that is true - I totally forgot to mention that in the TODO section of the arcticle. I’ll update it accordingly. Thanks for the reminder.
Comment by FiNeX (not verified) (2010-02-04 01:30:00)
Hi! I don’t remember if I’ve talked about snipplets with you in b.k.o. Did you asked to gedit devs if it could be possible to port their snipplets which works very well?
Comment by Milian Wolff (2010-02-04 11:26:00)
See TODO list ;-) I updated it.
Comment by shamaz (not verified) (2010-02-03 19:04:00)
a blog post without screencast is acceptable. but a blog post without screenshot is unacceptable ! ;)
Comment by Milian Wolff (2010-02-03 23:06:00)
well the real magic of the snippets handler cannot be shown with a simple screenshot, a screencast is required!
Comment by Dan Leinir Turthra Jensen (not verified) (2010-02-03 18:35:00)
This stuff is simply amazing! We’re really getting up to spec here :) Snippets is one of the final things that’s made Visual Studio more interesting, and well… now that just begins looking… damn yeah :)