DEV Community

Cover image for Enhance Your Productivity with Code-Snippets in VSCode
Hassan Suhaib
Hassan Suhaib

Posted on

Enhance Your Productivity with Code-Snippets in VSCode

I've been using a lot of Python27 recently and since there's no f-strings in it, I have to write a lot of code like this for debugging:

logging.info("This variable: {}".format(variable))
Enter fullscreen mode Exit fullscreen mode

which I really hate.

Using Code Snippets has really been helpful in this regard since I can just type any letter combination that I have configured and get a boilerplate of what I want to code.

E.g. on typing plog I get:

logging.info('${1:placeholder}: {}'.format(${1:placeholder}))
Enter fullscreen mode Exit fullscreen mode

and if I start typing after pressing enter, the placeholder 1:placeholder gets replaced with whatever I typed. Since in this example there are two placeholders, pressing enter will replace them both simulataneously.

If you have set a placeholder like 2:placeholder then you can just hit tab after you're done replacing 1:placeholder so that you can edit 2:placeholder.

Here's how you can do it in VSCode.
Open VSCode and press ctrl + shift + P. Type "Snippet: Configure Code Snippets" and hit enter.

Code Snippet Option VSCode

Select a language (I'll select Python).

Language Selection in Code Snippet

Then add snippets like so. Notice that each new line is a new entry in the body array.

Code Snippets Configuration

Save the file.

Now let's say I type imndb

Typing imndb

and press enter, it gets replaced with my saved code snippet.

imndb Code Snippet

Hope this was helpful in improving your workflow. Feel free to drop a comment and share with your friends.

Top comments (0)