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.

Great list @ben. I am using Emacs and YASnippet serves me well.

2 Likes

Wow wow wow https://github.com/SirVer/ultisnips#screencasts

My mind is expanded. I’m definitely adding these to my workflow - I knew this existed but didn’t realize how powerful it was. It looks like this can almost act as autocompletion in some cases, like for html tags and such.

@ben do you use any kind of autocompletion for Vim? I’ve been frustrated that it seems like OmniCompletion every time I’ve tried it either doesn’t work very well, is too slow, or just glitchy. I haven’t found it as necessary for dynamic languages like Python which I primarily use - Typescript support seemed fairly solid but I’ve never been able to get Python to work very well.

1 Like

I use a bit of the built-in autocompletion, but not much. Mostly just to tab-complete names.

I do like this plugin which lets me just mash tab and have it do the right thing most of the time.

Was previously using this extension for Python snippets: https://marketplace.visualstudio.com/items?itemName=cstrap.python-snippets

But found most of them useful/actually got in the way. As such I created my own, some of which were outright stolen from that page, others based on the ones on that page, others I’ve created myself:

  • pdb
  • env
  • enc
  • init
  • property
  • ifmain
  • pytraises
  • pytmdjango
  • pytfixture

If one wants them, here’s the json definitions:

{
	"pytestraises": {
		"prefix": "pytraises",
		"body": [
			"with pytest.raises(Exception):",
			"    "
		],
		"description": "Insert a pytest.raises() block"
	},
	"Pytest Mark Django": {
		"prefix": "pytmdjango",
		"body": [
			"pytestmark = pytest.mark.django_db"
		],
		"description": "Insert a pytestmark = pytest.mark.django_db"
	},
	"Pytest Fixture": {
		"prefix": "pytfixture",
		"body": [
			"@pytest.fixture",
			"def myfixture():",
			"    ",
			"    yield",
			"",
			""
		],
		"description": "Skeleton of a new pytest fixture"
	},
	"Insert PDB statement": {
		"prefix": "pdb",
		"body": [
			"import pdb; pdb.set_trace()"
		],
		"description": "Inserts a pdb import & set trace"
	},
	"env": {
		"prefix": "env",
		"body": [
			"#!/usr/bin/env python"
		],
		"description": "Inserts a python2 hashbang"
	},
	"env3": {
		"prefix": "env3",
		"body": [
			"#!/usr/bin/env python3"
		],
		"description": "Inserts a python3 hashbang"
	},
	"enc": {
		"prefix": "enc",
		"body": [
			"# -- coding=utf-8 --"
		],
		"description": "Insert a utf-8 file encoding line"
	},
	"init method": {
		"prefix": "init",
		"body": [
			"def __init__(self, *args, **kwargs):",
			"    pass"
		],
		"description": "Insert an __init__ stub"
	},
	"Create Property": {
		"prefix": "property",
		"body": [
			"@property",
			"def myproperty(self):",
			"    return self._myproperty",
			"",
			"@myproperty.setter",
			"def myproperty(self, value):",
			"    self._myproperty = value"
		],
		"description": "Creates a property with a getter & setter"
	},
	"Main method": {
		"prefix": "ifmain",
		"body": [
			"def main():",
			"    pass",
			"",
			"if __name__ == \"__main__\":",
			"    main()"
		],
		"description": "Creates an if main block"
	}
}
1 Like

Anyone got any “must have” snippets for ruby or rails their willing to share?

For Xcode/Swift users, this repo as a number of snippets to take inspiration from.

Also has a nice GIF showing how to create your own