Difference between revisions of "Yasnippet"

From WikEmacs
Jump to navigation Jump to search
(added yasnippet page)
 
m
 
(5 intermediate revisions by 4 users not shown)
Line 2: Line 2:
 
|name=yasnippet
 
|name=yasnippet
 
|description=template system for Emacs
 
|description=template system for Emacs
 +
|author=João Távora
 
|maintainer=João Távora
 
|maintainer=João Távora
 
|source=https://github.com/capitaomorte/yasnippet
 
|source=https://github.com/capitaomorte/yasnippet
 
}}
 
}}
  
'''YASnippet''' is a template system for Emacs. It allows you to type an abbreviation and automatically expand it into function templates. Bundled language templates includes: C, C++, C#, Perl, Python, Ruby, SQL, LaTeX, HTML, CSS and more.
+
'''YASnippet''' (Yet Another Snippet) is a template system for Emacs. It allows you to type an abbreviation and automatically expand it into function templates. Bundled language templates includes: C, C++, C#, Perl, Python, Ruby, SQL, LaTeX, HTML, CSS and more.
  
= Basic setup =
+
== Basic setup ==
  
 
<source lang="lisp">
 
<source lang="lisp">
Line 16: Line 17:
 
</source>
 
</source>
  
= Helpful keybindings =
+
== Helpful keybindings ==
  
 
; {{Keys|Tab}}
 
; {{Keys|Tab}}
 
: Expand snippet.
 
: Expand snippet.
  
= Common Customization =
+
== Embedded Lisp ==
 +
It is possible to embed lisp code directly in to snippet files by surrounding the
 +
s-expression with backticks ('''`'''). For example:
 +
<syntaxhighlight lang="lisp">
 +
#name : document string
 +
# --
 +
-------------------------------------------------------------------------------
 +
-- `(car (last (split-string (buffer-file-name) "/"))) ; finds last entry
 +
  ; (filename) in the full path of the file`
 +
-- Author: Author
 +
-- Description: $1
 +
-------------------------------------------------------------------------------
 +
</syntaxhighlight>
 +
This snippet will extract the name of the buffer (which is presumably the file
 +
name) and add it to the second line of the expansion. Yasnippet is smart enough
 +
to ignore the comment at the end of the line as well.
  
= See Also =
+
== Common Customization ==
  
= Project Pages =
+
=== Use Yasnippet with Hippie Expand ===
 +
 
 +
If you use [[Hippie Expand]], you can add [[Yasnippet]] to the list of expansion functions. For example if you want to try expanding snippets first:
 +
{{Snippet|(setq hippie-expand-try-functions-list (cons 'yas/hippie-try-expand hippie-expand-try-functions-list))}}
 +
 
 +
You could do this globally, or for a specific mode using a [[mode hook]].
 +
 
 +
{{Note|If you use {{Keys|Tab}} for [[Hippie Expand]], consider using a prefix for your snippets. Otherwise you may be trying to tab between possible expansions and find yourself tabbing through fields in an expanded snippet instead.}}
 +
 
 +
== See Also ==
 +
 
 +
== Project Pages ==
 
* [https://github.com/capitaomorte/yasnippet GitHub Repo]
 
* [https://github.com/capitaomorte/yasnippet GitHub Repo]
  
= Tutorial Pages =
+
== Tutorial Pages ==
 
* [http://yasnippet.googlecode.com/files/yas_demo.avi Screencast]
 
* [http://yasnippet.googlecode.com/files/yas_demo.avi Screencast]
  
[[Category:Convenience]][[Category:Third Party Package]]
+
[[Category:Intermediate]]
 +
[[Category:Convenience]]
 +
[[Category:Third Party Package]]
 +
[[Category:Template System]]
 +
[[Category:Editing]]
 +
[[Category:Popular Package]]

Latest revision as of 20:03, 18 October 2013

yasnippet
Description template system for Emacs
Author João Távora
Maintainer João Távora
Source https://github.com/capitaomorte/yasnippet

YASnippet (Yet Another Snippet) is a template system for Emacs. It allows you to type an abbreviation and automatically expand it into function templates. Bundled language templates includes: C, C++, C#, Perl, Python, Ruby, SQL, LaTeX, HTML, CSS and more.

Basic setup

(add-to-list 'load-path "~/.emacs.d/plugins/yasnippet")
(require 'yasnippet)
(yas/global-mode 1)

Helpful keybindings

[Tab]
Expand snippet.

Embedded Lisp

It is possible to embed lisp code directly in to snippet files by surrounding the s-expression with backticks (`). For example:

#name : document string
# --
-------------------------------------------------------------------------------
-- `(car (last (split-string (buffer-file-name) "/"))) ; finds last entry
   ; (filename) in the full path of the file`
-- Author: Author
-- Description: $1
-------------------------------------------------------------------------------

This snippet will extract the name of the buffer (which is presumably the file name) and add it to the second line of the expansion. Yasnippet is smart enough to ignore the comment at the end of the line as well.

Common Customization

Use Yasnippet with Hippie Expand

If you use Hippie Expand, you can add Yasnippet to the list of expansion functions. For example if you want to try expanding snippets first:

(setq hippie-expand-try-functions-list (cons 'yas/hippie-try-expand hippie-expand-try-functions-list))

You could do this globally, or for a specific mode using a mode hook.

Note: If you use [Tab] for Hippie Expand, consider using a prefix for your snippets. Otherwise you may be trying to tab between possible expansions and find yourself tabbing through fields in an expanded snippet instead.

See Also

Project Pages

Tutorial Pages