What the hell is a Snippet?

Have you ever seen any experienced programmers, typing code extremely fast? You look at their screens and wonder how lines of code just pop up, out of no where at the perfect time, at the perfect location? What the heck did they just do? It’s called Snippet.

Let’s talk about snippets for Ruby on Rails and erb file on VScode!

L: beginners: typing every single letter without snippets. 30sec. R: smart coders: using SNIPPETS and saving time. 10sec.
wiki definition of snippet

Definition of Snippets

A snippet is a small code that can be inserted into the code of a program. Snippets provide a quick path to implement commonly used code into a larger section of code. Instead of re-writing the same code over and over again, a programmer can save the code as a snippet and simply type the prefixwherever it is needed. Simply explained, let’s say, push the button or a shortcut .

when you type the prefix the commonly used code appears in your editor.

Anatomy of Snippets

Let me break the snippets down for you.

  • “Name”: Snippet is key-value pair. Nameis to identify which snippet is called.
  • ​“prefix”: the keystroke, or an input key that will trigger to insert the original code.
  • ​“body”: the original code that will be inserted when the snippet is called.
  • ​“description”: a description of what the code snippet contains.

How do I add snippets on VScode?

Open User Snippets to add snippets onto your VScode. Code > Preferences > User Snippets the type ruby.json to add snippets into ruby.json file.

$1, $2 are for tab stops. $0 is for the final cursor position. and ${1:label}, ${2:another} for placeholders.

Now you are an expert at Snippets!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store