在yasnippet中执行emacs脚本,实现动态获取时间戳等方法

对, 那么正则替换方式,可以在snippets片段中实现吗?
具体实现方式:在反引号( ` )执行emacs脚本

获取文件路径:`file-attributes (buffer-file-name)`

获取buffer名称:`(file-name-nondirectory buffer-file-name)` 

获取当前时间戳:
1. `(current-time-string)`
2. `(format-time-string "<%Y-%m-%d %H:%M>" (current-time))`

Emacs-Lisp code can be embedded inside the template, written inside back-quotes (`). The lisp forms are evaluated when the snippet is being expanded. The evaluation is done in the same buffer as the snippet being expanded.

Writing snippets

1 个赞

bu cuo ou

没太懂问题是什么,在yas中执行代码用command类型snippet更强大一些:

# -*- mode: snippet -*-
# name: cl (command)
# key: cl
# type: command
# --
(let ((in-js-p
       (save-excursion
         (move-beginning-of-line 1)
         (looking-at "^[ \t]*$"))))
  (yas-expand-snippet
   (if in-js-p "console.log($0);" "className=\"$0\"")))
4 个赞

求详解, command 类型怎么使用?

看例子,type:command,下面写elisp代码就完事,文档里:

If the type directive is set to command , the body of the snippet is interpreted as lisp code to be evaluated when the snippet is triggered.