Custom yasnippet Template

Source: Internet
Author: User
Tags shebang

Yasnippet can store frequently-used code segments or texts, which are automatically taken out by typing a few letters.

For example, when writing Python code, we often write in the first line of the file :#! /Usr/bin/ENV python, It is very inefficient to manually type it like this. Use yasnippet to help you!

Method:

1. Create an snippet

Run the M-x Yas-New-snippet command to open a new buffer, or directly create a file. Enter the content and save it to your specified location.

The buffer content opened with m-x Yas-New-snippet is as follows by default:

# -*- mode: snippet; require-final-newline: nil -*-# name:# key:# binding: direct-keybinding# –

You only need the following content:

# name: file header# key: fh# binding: direct-keybinding# --#!/usr/bin/env python# coding:utf-8# Filename:`(file-name-nondirectory buffer-file-name)`$0

Explanation

Name: File Header: This is the clip name, which is called file header. This file header is displayed in yasnippet on the menu bar of Emacs.

Key: FH: This is the shortcut key. Enter the FH in the file and press the tab key to display the clip.

From # -- enter the code snippet below.

Filename: '(file-name-nondirectory buffer-file-name )'This is interesting. It shows the name of the Current Buffer and enables Emacs to help you write it automatically.

 

$0 indicates the position of the cursor after the fragment is expanded.

Ii. Save

Where can I save snippet after writing it? Generally, it can be saved to the default installation location of yasnippet (. emacs. d/elpa/yasnippet-20140729.1240/snippets), or a custom location, such :~ /. Emacs. d/custom-snippets. If it is saved to the default location, it can be used without special settings. If it is saved to a custom location, you need to tell the Emacs in the. emacs configuration file where to find this segment:

; Yasnippet (require 'yasnippet); set the snippet directory (setq Yas-snippet-dirs '("~ /. Emacs. d/custom-snippets "; personal snippets "~ /. Emacs. d/elpa/yasnippet-20140729.1240/snippets "; default ;"~ /. Emacs. d/elpa/yasnippet-20140720.1534/snippets "; default) (Yas-global-mode 1)

Note that the directory name of yasnippet will change after you update it. Currently, there is no way to manually change this path after each update.

Finally, you must create a python-mode directory in the Custom M m-snippets directory! Put the created file in it. Iii. Use

Create a new Python file, type FH in the first line of the file, and press the tab key. The result is as follows:

 

-- End --

Appendix:

#! /Usr/bin/ENV python is shebang line

Http://stackoverflow.com/questions/2429511/why-do-people-write-usr-bin-env-python-on-the-first-line-of-a-python-script

In computing, a shebang (also called a hashbang, hashpling, pound bang, or crunchbang) refers to the characters "#! "When they are the first two characters in an interpreter directive as the first line of a text file. in a Unix-like operating system, the program loader takes the presence of these two characters as an indication that the file is a script, and tries to execute that script using the interpreter specified by the rest of the first line in the file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.