Lima, the capital of Perú, is known for its amazing food. If you’re ever around, even if only for a layover — such as my case right now — you can’t leave without trying a Cebiche. This one is from Tanta, and I’m enjoying it while releasing a new version of Create Block Theme.
Author: Vicente
-
Streamlining Your Coding Workflow with VS Code Snippets
Visual Studio Code (VS Code) is an extraordinarily versatile text editor favored by developers worldwide. Among its many powerful features, one stands out for its ability to streamline repetitive coding tasks: custom snippets. In this blog post, we’ll explore how you can leverage custom snippets across various languages, including JavaScript/React, Node.js, PHP, Python, and Ruby.
What are VS Code Snippets?
VS Code snippets are templates that make it easier to enter repeating code patterns, like loops or conditional statements. They’re essentially shortcuts you define that, when triggered, produce a block of code you frequently use. Snippets can dramatically increase your productivity by reducing the amount of repetitive typing and the potential for typographical errors.
Creating Custom Snippets
The creation process for custom snippets in VS Code is quite straightforward. Here’s a general breakdown:
- Press
CMD/Ctrl-Shift-P
to open the command palette. - Type “Configure User Snippets” and select it.
- You’ll be prompted to select which language this snippet should apply to. Choose the language you want to create a snippet for.
- VS Code will open a JSON file where you can define your snippets for the selected language.
Of course, let’s add a section highlighting the initial use case:
PHP Snippet for HTML Escaping
Now, let’s consider a very specific but common task in WordPress and PHP development, especially when dealing with output sanitization for WordPress: wrapping text in an
esc_html__()
function for secure output.Normally, you would need to type something like this each time:
<?php echo esc_html__( 'your_text', 'domain' ); ?>
. However, we can create a custom snippet in VS Code to handle this operation, making it as simple as a few keystrokes:{ "Translate and Escape": { "prefix": "escapehtml", "body": [ "<?php echo esc_html_( '${TM_SELECTED_TEXT}', '$2' ); ?>" ], "description": "Escape HTML strings in PHP" } }
With this snippet, all you need to do is highlight the text you want to wrap and then type the prefix
escapehtml
. VS Code will replace your selected text with the entire PHP escape function, with your selected text safely inserted in the correct place.It’s scenarios like these where VS Code custom snippets shine by significantly speeding up your development process and ensuring consistency in your code. Remember, these snippets can be personalized to meet the unique demands of your projects, enabling you to code faster and more efficiently.
Let’s dive into specific examples for various languages:
JavaScript/React Snippet
Imagine you’re working on a React project and frequently need to create new components. Here’s a snippet for a functional component:
{ "React Functional Component": { "prefix": "rfc", "body": [ "import React from 'react';", "", "const ${1:ComponentName} = () => {", " return (", " <div>${TM_SELECTED_TEXT}</div>", " );", "};", "", "export default ${1:ComponentName};" ], "description": "React Functional Component" } }
Node.js Snippet
If you frequently write asynchronous functions using async/await in Node.js, this snippet can speed things up:
{ "Async Function": { "prefix": "asyncfunc", "body": [ "async function ${1:functionName}() {", " try {", " ${TM_SELECTED_TEXT}", " } catch (error) {", " console.error(error);", " }", "}" ], "description": "Async Function" } }
PHP Snippet
Suppose you frequently work with associative arrays in PHP; here’s a snippet that might be handy:
{ "PHP Associative Array": { "prefix": "phparray", "body": [ "$${1:arrayName} = array(", " '${2:key}' => '${TM_SELECTED_TEXT}',", ");" ], "description": "PHP Associative Array" } }
Python Snippet
Working with classes in Python? Here’s a snippet for a class with an initializer method:
{ "Python Class": { "prefix": "pyclass", "body": [ "class ${1:ClassName}:", " def __init__(self, ${2:params}):", " ${TM_SELECTED_TEXT}", "" ], "description": "Python Class" } }
Ruby Snippet
If you’re dealing with blocks in Ruby, this snippet can save you some typing:
{ "Ruby Block": { "prefix": "rubyblock", "body": [ "${1:array}.each do |${2:element}|", " ${TM_SELECTED_TEXT}", "end" ], "description": "Ruby Block" } }
Conclusion
With these examples, you should understand how powerful and versatile VS Code snippets can be. Remember, the power of snippets extends beyond the languages discussed – you can create custom snippets for virtually any language supported by VS Code. By leveraging this feature, you can significantly enhance your coding efficiency and make your development process smoother and more enjoyable. Happy coding!
- Press
-
Cooking together
For New Year’s I spent a few days up in the mountains with my closest people. It was a long weekend of cooking, eating, playing, and laughing. Getting away from the noise of the city and going into nature to make our own is a great way to reset and start the new year with a renewed perspective of what matters most.
-
So coffee in Porto
2022 brought me to many places, and this café in Porto is one of them. I took beans back home, some of which were from Latin America; I can’t help but find it funny that I’m bringing Latin American beans back to the New World.
-
Jazz and wine
Nights filled with rage, with unsatisfied desires Jazz unwinds, wine accelerates the mind unfinished tasks neglected talks dislocated looks crooked smiles Stare at the saxophone, fixate on the ride, ride, ride I can’t tell whether this is in my mind or outside focus, do I recognize this phrase? do I recognize this voice? don’t miss your turn, jump in, sing, sing, sing.
-
Jump into a system, or do the thing wrong first?
I’m overwhelmed by big frameworks and systems. So much so that it feels like I spend more time thinking about how to do the thing right than actually doing something.
I’m going through this right this second as I research for a blog post; I’m wondering if I should be using Notion to keep track of the list of articles that I want to read and whether or not I’ve read them or not. I know myself, and, likely, those articles will sit collecting digital dust on a Notion page.
Right this second, I’m looking back at the entry and thinking, “well, I’m going to keep working on this, so I should move it to Notion, where documents are not necessarily meant to be tied to a date… come on!
I’m not going to do it. This time I’ll start the wrong way, go back to the entry, and edit it until doing so becomes a nuisance, or I publish it, whichever comes first.
-
Buena onda
No des nada, por favor, almenos (sic) que debas realmente. Gracias. Si piensas que te debo algo por el disturbio que mi música te causa, y porque un anciano raro como yo no debería tocarlo, y más bien debería estar con pensión y sedado en un asilo, solo tómalo de la caja del saxo. Gracias.
-
Rat race
People sometimes forget that a rat race can be won only by a rat. A colorful wall on New Orleans’ Frenchman Street.
-
Selfie
I like taking selfies I’m not a part of