Day 25 - Create/update your snippets

(This challenge was again created by guest contributor Giovanni Lodi.)

Today’s challenge: spend 20 minutes getting snippets support configured in your editor. (If you’ve already done this, then spend some time adding a new one or two. Maybe delete some old ones you never use. Tend your snippet garden.)

I think it’s worth being fairly intolerant of unnecessary typing. It’s slow, error-prone, and puts mileage on your hands.

Let’s type less and get more done faster.

Here are some places to start. How to make snippets in…

If your development environment doesn’t support snippets, you can use a third party tool to bring snippets to all apps. On macOS, Alfred and Dash are great.

I’m using Rocket Typist to have snippets available everywhere on the machine.

I spend this morning going through them to clean up and adjust.

1 Like

I renamed some of my UltiSnip Ruby snippets as I kept forgetting how to invoke them. Hopefully the new names will stick.

1 Like

Good one, I had forgotten how many “Live Templates” I actually use, and that I can modify them myself.
Using IntelliJ-products mean I’m pretty used to typing a few letters of the class name and hit enter - but this can do so much more!

I use PhpStorm frequently with Laravel, so I’ve set up some snippet/templates around the Collection framework.

1 Like

Great reminder! I used to have a couple of them in IntelliJ some years ago and must admit I forgot about it. Since I’m now starting to use IntelliJ more again because of a new project I joined recently I had to look up the syntax again. The main one I use is @Test<TAB> to scaffold a new JUnit test method placing the cursor at the method name and then in the method body (i.e. $END$). After a couple of tries I have that one working again. I wish there was a simple way to extract these custom snippets so that I can commit them with my dotfiles.

Yes!

That yasnippet video was lying around in my chrome tabs faaaaar too long:

1 Like

I don’t care for things meddling with my typing. I find typing is not the slow part of programming – the thinking takes much longer.

That said, there are a few things that require annoying boiler plate (nested_attributes I’m looking at you). So I’ve taken today’s task as a documentation goal adding better examples that we can snag from our internal libraries.

1 Like

I’m going to put hands on tailwind css, so I installed several extensions to make learning easier. Surprisingly, I was not a fan of snippets and autocompletions for historical reasons. Maybe, It’s a good chance to change some habits. will also check other tools.

1 Like

That’s a pretty neat app, macOS development at his best: a small, clean, highly focused tool. Thank you for sharing.

typing is not the slow part of programming – the thinking takes much longer.

That’s absolutely true. One could argue that investing into sharpening our thinking tools, e.g. by exploring a new programming language to see how they do things differently or reading about mental models.

Still, I’d argue that investing a bit of time into removing friction from the process of materializing your thinking into code, words, actions is useful, too.

1 Like

Not being an Emacs user, I didn’t know about yasnippet. I’ll add it to the list. Thanks.

How do you use Emacs? As in, what do you, Julian, use it, not how to use it general :sweat_smile:. What do you find useful as opposed to using a text editor like VSCode and going to terminal just to run commands?

I’ve tried a few times but never go into it.

Emacs is clearly a hobby and it will consume you.

I probably used any combination from full blown IDE to sublime to vim. Actually I‘m still using vim keybindings.

Honest to God, I cannot really tell you the exact reason why I use it, I‘m not even a good emacser. It’s probably a combination of strong muscle memory (I‘m a musician), extensibility to the max, integrations and features.

For example I never wrote one line of Vimscript. Then, three weeks into my emacs usage I wrote a custom function to trigger my Harvest timer and annotate it with the current git branch and project.

Does that answer your question? :joy:

I’m all for sharpening tools mental & mechanical. And for everyone who enjoys snippets, more power to ya. I’m a long term vim user and try to actively practice, that is the way that I focus on reducing friction when materializing code – there are many paths to less friction.

Also, I’ll second your recommendation of that mental models site. I’ve really enjoyed it. Great to mention here!

1 Like

I have (as using neovim) UltiSnips ready to use for quite some time. But I am not used to use the snippets.

Today I did some review and:

  • found, that some customized snippets (tox.ini, setup.cfg and few more) are there and are very powerful
  • moved snippets to proper place
  • checked my nvim config to find out, how to jump around snippet fields

I will try to use snippets where appropriate.

I used the time to figure out how snippets can be added to VScode and created some html snippets to layout some basic structures I often need.

BTW: if you don’t have snippets for your email address, name, and mailing address, you’re typing way more than you have to! :slight_smile:

4 Likes

Noone mentioned Intellij yet? There they are called 'live templates", I recently blogged about it: Live Templates - larseckart.com

Had never really used snippets before. I knew that they were there… But hadn’t got around to investigating them. Thanks for the prompt the read the IDE manual again and get some learning in.

I was reading and experimenting with snippets (live templates) in Intellij.

I added this snippet to my Dart collection in VSCode for a singleton which I often need:

	"Service": {
		"prefix": "service",
		"description": "Singleton service class with async constructor",
		"body": [
			"class ${1}Service {",
			"  static ${1}Service? _instance;",
			"",
			"  ${1}Service._() {}",
			"",
			"  static Future<${1}Service> create() async {",
			"    if (_instance == null) {",
			"      _instance = ${1}Service._();",
			"    }",
			"",
			"    return _instance!;",
			"  }",
			"}",
		]
	}