Site icon DesignLinux

How to Boost the Productivity with Sublime Text Snippets

Long story short, Recently I was assigned to a project in my work where I have to create a lot of bash scripts. I am from a python background and using Jupyter Notebook for all my development work. The problem with bash scripts for me is its confusing bracket usage and repetitive block of codes across all my scripts.

Till that point, I was using Nano editor to create bash scripts. Nano provides some basic features as a terminal editor but not beyond a point. So I did my research and selected “SUBLIME TEXT 3” as my go-to editor for bash and other programming languages. I created a lot of snippets for repetitive functions, one-liners, and control blocks for bash scripts which not only saved time but also improved my productivity.

What is Snippet?

Snippets are a popular programming feature/functionality that ships with many modern text editors or IDE editors. You can think of snippets as a template that can be reused whenever required. Snippets are not restricted to particular programming languages. You can simply create a new snippet, add any text you want to be inserted and assign a trigger word. We will see all these features in the upcoming section.

NOTE: I am using Sublime Text 3 with Ubuntu 20.04 as my base OS.

To List Defined Snippets in Sublime Text

By default sublime text ships with some predefined snippets for the bash. It will smartly display the snippets based on the current file we are searching for the snippet from. I am inside a shell script and when I invoke the command pallet and type snippet, it automatically provides a list of defined snippets for the bash.

List Default Snippets

There are two ways you can access snippets in Sublime Text.

  1. MENU DRIVEN ⇒ SUBLIME TEXT → TOOLS → SNIPPETS
  2. COMMAND PALETTE ⇒ SUBLIME TEXT → COMMAND PALETTE (CTRL+SHIFT+P) → TYPE SNIPPETS

Create a New Snippets in Sublime Text

Sublime text provides a default template in XML format when we create a new snippet. To create the template got to SUBLIME TEXT → TOOLS → DEVELOPER → NEW SNIPPET.

Create New Snippets

Let’s understand the template definition and modify the parameters.

Now we have done some basic stuff. We have defined a snippet that will insert a simple header comment which binds with the “hcom” tab trigger and sourced to shell script.

Custom Snippet

Now let’s open a new bash file and “type hcom“. If you look at the below image when I “type h” my snippet definition just pops up with the description we gave. All I have to do is press the <tab> key to expand it.

Snippet hcom

Fields

Fields are denoted using $1, $2, $3 and so on. With the help of the field, you can jump to the position where the field marker is placed by simply hitting <tab> key.

If you take a look at my snippet I have added two field markers $1 and $2, what it does is when I insert my snippet the cursor will be placed at $1 so I can type something at that position.

Then I have to press the <tab> key to jump to the next marker $2 and type something. It is to be noticed that when you have any identical marker say $1 in this case at 2 positions, updating the field at one position will update the identical fields ($1).

Place Holder

Place holders are like a key-value pair defined within curly braces ${0:<Default value>}; the field marker will be tagged with a default value. You can either change the value or leave it as it is. When the snippet is inserted and if you press the tab the cursor will be placed at the default value.

Placeholder Snippet

Now the snippet is inserted with the default value and the mouse is placed at $1 which is v1 in this case. Either I can modify the value or just press <tab> the key to move to the next marker.

Default Values

The only drawback with Sublime Text snippets are, you cannot group all the snippets in a single file. Only one snippet per file is allowed which is cumbersome. But there are other options like creating .sublime-completion files. To know more about this, take a look at the documentation.

Where is My Snippet File Stored?

The snippet files should be saved with the suffix .sublime-snippet. Go to PREFERENCESBROWSE PACKAGES. It will open the directory where user-defined settings are stored. Go to the directory “User” where your snippet file will be saved.

Snippet Location

Snippet Generator

Snippet-generator-app provides web-based automatic snippet generation based on our input. It is available for Sublime Text, Atom, and VSCode. Type the description, tab trigger, and content on the left side which will generate live code at the right side of the page.

Sample snippet that will get Cluster name from Ambari API.

Sublime Text Snippet Code Generator
Generated Snippet

That’s all for today. We have seen the advantages of using snippets in sublime text. I have just used a simple pseudo text as an example to demonstrate the snippets feature but there is a lot more to it. I would also point out this feature is available in all the editor/IDE like Vim, Atom, Eclipse, Pycharm, Vscode, etc.

Exit mobile version