Atom Editor Code Snippet: Efficient Code Reuse Tool
Code snippets are reusable code blocks that can quickly insert program files and are the core function of Atom text editor. Predefined fragments are usually provided with Atom packages and language syntax.
Custom snippets can be created and defined in the ~/.atom
file located in your snippets.cson
folder. They require language identifiers, names, trigger text, and fragment body code (optional tabs).
Snippets can be used in any programming language supported by Atom, just specify the correct scope of the language when defining the fragment. They can contain variables and can be used to insert common code blocks, saving time and ensuring code consistency.
Code snippets are common code blocks that you can quickly insert program files. They are very useful and are also the core feature of the Atom text editor. That is, you can use the editor for months without realizing the existence of code snippets or experiencing their power! Atom packages and language syntax usually come with predefined code snippets. For example, start or open a new HTML file, then type img and press Tab. The following code will appear:
<img src="" alt="" />The
cursor will be located between the src
attribute quotes. Press the Tab key again and the cursor will move to the alt
property. Press the Tab key for the last time and the cursor will move to the end of the label. When you start typing, the code snippet trigger text is indicated with a green arrow. You can view all defined code snippets of the current file language type by putting the cursor anywhere and pressing Alt-Shift-S. Scroll or search the list to find and use specific code snippets. Alternatively, open the Package list in Settings and enter language to see a list of all syntax types. Select one and scroll to the Code Snippet section to view predefined triggers and code.
How to create your own code snippet
You will have your own commonly used code blocks that can be defined as code snippets. The useful command I use when debugging a Node.js application is to log the object as a JSON string to the console:
console.log('%j', Object);
Atom already has a predefined log
trigger for console.log()
; however, you can improve it with custom code snippets. Custom code snippets are defined in the ~/.atom
file located in your snippets.cson
folder. Select Open your code snippet from the File menu to edit it. The code snippet requires the following information:
- Language identifier or rangeString
- Simplely describe the name of the code
- Once the Tab key is pressed, the trigger text of the code snippet will be triggered, and
- Code snippet body code with optional tabs.
Go to the end of snippets.cson
, type snip
and press Tab - Yes, there is even a code snippet that can help you define the code snippet! …
<img src="" alt="" />
Note that CSON is a CoffeeScript object notation. It is a concise syntax that can be mapped directly to JSON; essentially, use indentation instead of {} brackets. First, you need a range string that identifies the language in which the snippet of code can be applied. The easiest way to determine the scope is to open the Package list in Settings and enter "language". Open the required syntax package and look for the "Scope" definition near the top.
The code snippet range insnippets.cson
must also add a period at the beginning of the string. Popular Web language ranges include:
- HTML:
.text.html.basic
- CSS:
.source.css
- SASS:
.source.sass
- JavaScript:
.source.js
- JSON:
.source.json
- PHP:
.text.html.php
- Java:
.source.java
- Ruby:
.text.html.erb
- Python:
.source.python
- Plain text (including Markdown):
.text.plain
Therefore, you can define a JSON logging code snippet using the following method:
console.log('%j', Object);
Once your snippets.cson
file is saved, the code snippet will take effect. In this example:
- Scope is set to
.source.js
(for JavaScript) - The code snippet is named "log JSON"
- Tab trigger (prefix) is set to
lj
- The body of the code snippet is set to
console.log('%j', Object);
(However, we have added some extra control code as shown below).
Single quotes in the body must be escaped with a backslash(). Tab stops are defined using a dollar sign followed by a number, i.e. $1, $2, $3, etc. $1 is the first tab position where the cursor appears. When the Tab key is pressed, the cursor will move to $2, and so on. The above tab stop $1 has been defined using the default text to remind or prompt the user: ${1:Object}
. When using a code snippet, Object
is selected in console.log('%j', Object);
, so it can be changed to the appropriate object name.
Other code snippets can be added to the bottom of the snippets.cson
file. If you need two or more code snippets of the same language, add them to the corresponding scope section. For example, you can create another JavaScript code snippet in the scope of .source.js
to record the length of any array:
'.source.js': 'Snippet Name': 'prefix': 'Snippet Trigger' 'body': 'Hello World!'
Please note that there are two ${1:array}
tags. When console.log('array length', array.length);
appears, you will see two cursors and both instances of array
will be highlighted - you just type the array name once and both will change!
Multi-line code snippet
If you feel more adventurous, you can use three double quotes """ to define a longer multi-line code snippet at the beginning and end of the body code. This code snippet generates a 2×2 with a single header line. Table:
<img src="" alt="" />
Code indentation inside the body of the code snippet has no effect on the CSON definition, but I recommend that you indent it outside the body definition for greater readability. I wish you a happy writing of the code snippet! If you are new to Atom, you should also refer to 10 essential Atom add-ons for recommended packages.
FAQ on using code snippets in Atom
How to create a new code snippet in Atom?
Creating new code snippets in Atom is a simple process. First, you need to open the code snippet file by going to the File menu and then to the Code Snippet. This will open a .cson file where you can define the code snippet. Each code snippet begins with a line .source
that specifies the language it applies to, followed by the code snippet name in quotes. You then define the prefix that will trigger the code snippet and the body of the code snippet itself. The body can be multiple lines and use the ${1:default_text}
syntax to specify tabs.
How to use code snippets in Atom?
To use code snippets in Atom, you just type the prefix defined for the code snippet and press the "Tab" key. This inserts the body of the code snippet at the cursor. If your code snippet has tabs, you can use the "Tab" key to move between them and enter the text you want.
Can I use code snippets in any programming language in Atom?
Yes, you can use code snippets for any programming language supported by Atom. You just need to specify the correct scope of the language when defining the code snippet. For example, for JavaScript you will use .source.js
and for Python you will use .source.python
.
How to share my code snippet with others?
If you want to share your code snippet with others, you can simply share your snippets.cson
file. This file contains all your code snippet definitions and can be found in your Atom configuration directory. Alternatively, you can create a package with code snippets and publish it to the Atom package repository.
Can I use code snippets to insert commonly used code blocks?
Absolutely! Code snippets are a great way to insert common code blocks. You can define a snippet for any snippet you type frequently and then insert it with just a few keys. This can save you a lot of time and help ensure consistency in your code.
How to edit existing code snippets in Atom?
To edit an existing code snippet in Atom, you need to open the snippets.cson
file and find the code snippet to edit. You can then modify the prefix, body, or scope as needed. When you are done, remember to save the file to apply the changes.
Can I use variables in code snippets?
Yes, you can use variables in your code snippet. The variable is represented by ${1:default_text}
, where "1" is the tab and "default_text" is the default text to be inserted. You can use variables to create placeholders in code snippets so that you can quickly fill in these placeholders when inserting code snippets.
How to delete code snippets in Atom?
To delete a code snippet in Atom, you need to open the snippets.cson
file and find the code snippet to be deleted. Then, just delete the line of code that defines the code snippet and save the file. The code snippet will be deleted immediately.
Can I import code snippets from other editors into Atom?
Although Atom does not have built-in functionality to import code snippets from other editors, you can manually copy snippet definitions from other editors and paste them into the snippets.cson
file in Atom. You may need to tweak the syntax a little to match Atom's code snippet syntax.
Can I use code snippets in Atom's find and replace functions?
Yes, you can use code snippets in Atom's Find and Replace features. When you open the Find and Replace panel, you can enter a code snippet in the Replace field. When you perform a replacement operation, the code snippet is inserted into the location of the found text.
The above is the detailed content of How to Use Code Snippets in Atom. For more information, please follow other related articles on the PHP Chinese website!

The rise of Chinese women's tech power in the field of AI: The story behind Honor's collaboration with DeepSeek women's contribution to the field of technology is becoming increasingly significant. Data from the Ministry of Science and Technology of China shows that the number of female science and technology workers is huge and shows unique social value sensitivity in the development of AI algorithms. This article will focus on Honor mobile phones and explore the strength of the female team behind it being the first to connect to the DeepSeek big model, showing how they can promote technological progress and reshape the value coordinate system of technological development. On February 8, 2024, Honor officially launched the DeepSeek-R1 full-blood version big model, becoming the first manufacturer in the Android camp to connect to DeepSeek, arousing enthusiastic response from users. Behind this success, female team members are making product decisions, technical breakthroughs and users

DeepSeek released a technical article on Zhihu, introducing its DeepSeek-V3/R1 inference system in detail, and disclosed key financial data for the first time, which attracted industry attention. The article shows that the system's daily cost profit margin is as high as 545%, setting a new high in global AI big model profit. DeepSeek's low-cost strategy gives it an advantage in market competition. The cost of its model training is only 1%-5% of similar products, and the cost of V3 model training is only US$5.576 million, far lower than that of its competitors. Meanwhile, R1's API pricing is only 1/7 to 1/2 of OpenAIo3-mini. These data prove the commercial feasibility of the DeepSeek technology route and also establish the efficient profitability of AI models.

Website construction is just the first step: the importance of SEO and backlinks Building a website is just the first step to converting it into a valuable marketing asset. You need to do SEO optimization to improve the visibility of your website in search engines and attract potential customers. Backlinks are the key to improving your website rankings, and it shows Google and other search engines the authority and credibility of your website. Not all backlinks are beneficial: Identify and avoid harmful links Not all backlinks are beneficial. Harmful links can harm your ranking. Excellent free backlink checking tool monitors the source of links to your website and reminds you of harmful links. In addition, you can also analyze your competitors’ link strategies and learn from them. Free backlink checking tool: Your SEO intelligence officer

Midea will soon release its first air conditioner equipped with a DeepSeek big model - Midea fresh and clean air machine T6. The press conference is scheduled to be held at 1:30 pm on March 1. This air conditioner is equipped with an advanced air intelligent driving system, which can intelligently adjust parameters such as temperature, humidity and wind speed according to the environment. More importantly, it integrates the DeepSeek big model and supports more than 400,000 AI voice commands. Midea's move has caused heated discussions in the industry, and is particularly concerned about the significance of combining white goods and large models. Unlike the simple temperature settings of traditional air conditioners, Midea fresh and clean air machine T6 can understand more complex and vague instructions and intelligently adjust humidity according to the home environment, significantly improving the user experience.

DeepSeek-R1 empowers Baidu Library and Netdisk: The perfect integration of deep thinking and action has quickly integrated into many platforms in just one month. With its bold strategic layout, Baidu integrates DeepSeek as a third-party model partner and integrates it into its ecosystem, which marks a major progress in its "big model search" ecological strategy. Baidu Search and Wenxin Intelligent Intelligent Platform are the first to connect to the deep search functions of DeepSeek and Wenxin big models, providing users with a free AI search experience. At the same time, the classic slogan of "You will know when you go to Baidu", and the new version of Baidu APP also integrates the capabilities of Wenxin's big model and DeepSeek, launching "AI search" and "wide network information refinement"

This Go-based network vulnerability scanner efficiently identifies potential security weaknesses. It leverages Go's concurrency features for speed and includes service detection and vulnerability matching. Let's explore its capabilities and ethical

AI Prompt Engineering for Code Generation: A Developer's Guide The landscape of code development is poised for a significant shift. Mastering Large Language Models (LLMs) and prompt engineering will be crucial for developers in the coming years. Th


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

WebStorm Mac version
Useful JavaScript development tools